package com.health.fragment.focusgroup;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.health.tablebean.BD_DD_DRUG;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.tablebean.SBCL_DATA;
import com.health.tools.ImageTools;
import com.health.viewscontrols.FWMYDPJActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.CheckBoxAdapter;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.viewscontrols.focusgroup.GXYActivity;
import com.health.viewscontrols.focusgroup.TNBActivity;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.ARCHIVE_GENERALCONDITION;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.viewscontrols.medical.adapter.SfDrugAdapter;
import com.health.viewscontrols.qyinfo.QianMingActivity;
import com.health.viewscontrols.settings.QueryDrugListActivity;
import com.health.viewscontrols.tjbg.CameraActivity;
import com.health.viewscontrols.tupian.ClipActivity1;
import com.health.widget.EditTextWithDate;
import com.health.widget.MyGridView;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.views.ExamHistoryActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import org.json.JSONObject;

/**
 * 重点人群-- 糖尿病---随访
 *
 */
public class TNB_SfFragment extends NovaFragment implements onFragmentSave, OnClickListener {
	private static final String TAG = "TNB_SfFragment";
	public static final int RequestXueya = 0;
	public static final int RequestTizhong = 1;
	public static final int RequestXuetang = 2;
	private View cqsf2View = null;
	private FDbManager fdbmanger = null;

	// ------------------------------基本信息-----------------------------
	private EditTextWithDate ed_date;
	private NiceSpinner ed_sfys;// 随访医生
	private NiceSpinner nice_spinner_szfs;// 选择随诊方式

	private MyGridView gd_zz;// 症状

	private EditText ed_xy;// 血压
	private Button btn_choose_xy;

	private EditText ed_tz;// 体重
	private Button btn_choose_tz;// 选择体重
	private EditText ed_xcsfmb_tz;// 下次随访目标 体重

	private EditText ed_tzzs;// 体质指数
	private EditText ed_height;// 身高
	private EditText ed_tzzs_sf;// 体质指数 随访
	private EditText ed_qt;// 其他
	private EditText ed_rxyl;// 日吸烟量
	private EditText ed_xcsfmb_xy;// 日吸烟量 随访
	private EditText ed_ryjl;// 日饮酒量

	private EditText ed_xcsfmb_yj;// 日饮酒量 随访
	private EditText ed_yd_zq;// 运动周
	private EditText ed_yd_min;// 运动分钟
	private EditText ed_xcsfmb_yd_zq;// 运动周
	private EditText ed_xcsfmb_yd_min;// 运动分钟

	private EditTextWithDate ed_jcrq;// 检查日期
	private NiceSpinner nice_spinner_xltz;// 心理调整
	private NiceSpinner nice_spinner_zyxw;// 遵医行为

	private NiceSpinner nice_spinner_fyycx;// 服药依从性
	private MyGridView gd_ywblfy;// 药物不良反应

	private NiceSpinner nice_spinner_clsffl;// 此类随访分类
	private NiceSpinner nice_spinner_dtfy;// 低糖反应

//	private AutoCompleteTextView ed_ywmc1;// 药物名称1
//	private EditText ed_ywmc1_day;// 每日 day
//	private EditText ed_ywmc1_mg;// 每次 mg
//	private AutoCompleteTextView ed_ywmc2;// 药物名称2
//	private EditText ed_ywmc2_day;// 每日 day
//	private EditText ed_ywmc2_mg;// 每次 mg
//	private AutoCompleteTextView ed_ywmc3;// 药物名称3
//	private EditText nice_spinner_dw1;// 药物单位1
//	private EditText nice_spinner_dw2;// 药物单位2
//	private EditText nice_spinner_dw3;// 药物单位3
//	private EditText ed_ywmc3_day;// 每日 day
//	private EditText ed_ywmc3_mg;// 每次 mg
	private EditText ed_zs;// 主食
	private EditText ed_xcsfmb_zs;// xiaci主食
	private EditText ed_kfxtz;// 空腹血糖
	private Button btn_choose_kfxtz;
	private EditText ed_thxhdb;// 糖化血红蛋白
	private EditText ed_jcrq_intro;// 检查日期
	private EditText ed_fzjc;// 检查日期

	private AutoCompleteTextView ed_qtyw;// 胰岛素1
	private AutoCompleteTextView ed_qtyw2;// 胰岛素2
	private AutoCompleteTextView ed_qtyw3;// 胰岛素3

	private EditText ed_qtyw_mg;// 每次 mg
	private EditText ed_qtyw_mg2;// 每次 mg
	private EditText ed_qtyw_mg3;// 每次 mg
	private EditText ed_zzyy;// 转诊原因
	private EditText ed_jgkb;// 机构及科别
	private EditTextWithDate ed_xcsfsj;// 下次随访时间
	// ----------------------------------------------------------------------
	private RadioButtonAdapter adapter_ywblfy = null;

	private CheckBoxAdapter adapter_zz = null;
	private List<BD_DD_INFO> zbdm_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> dtfy_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> szfs_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> xltz_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> zyxw_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> ywycx_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> ccsffl_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> ywdw_list = new ArrayList<BD_DD_INFO>();
	public ARCHIVE_BASEINFO userInfo;
	private CD_DIABETESFOLLOWUP followUp;
	private NiceSpinner nice_spinner_zbdmbd;
	private CD_DIABETESFOLLOWUP readFollowup;
	// private static String strWhere = "IDCARD = '" +
	// NovaApplication.getUserInfo().getIDCARD() + "'";
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private int from_followup = -1;
	// 定义字符串数组，作为提示的文本
	String[] tnb_yws = new String[] { "盐酸二甲双胍肠溶片", "盐酸二甲双胍片", "消渴丸", "盐酸格列本脲片", "格列齐特缓释片", "盐酸格列美脲片", "格列吡嗪片",
			"盐酸吡格列酮片", "盐酸二甲双胍格列本脲片", "盐酸二甲双胍格列本脲胶囊", "瑞格列奈片", "阿卡波糖片", "胰岛素", "格列齐特片", "格列喹酮片", };

	List<ARCHIVE_GENERALCONDITION> ags;
	RadioButton rb_cjzc;
	RadioButton rb_jr;
	RadioButton rb_xs;
	RadioButton rb_sc1;
	RadioButton rb_sc2;
	RadioButton rb_zc1;
	RadioButton rb_zc2;
	RadioButton rb_yc1;
	RadioButton rb_yc2;
	RadioGroup rg_jr;
	RadioGroup rg_xs;

	private RadioGroup zhuanzhen_group;//是否转诊
	private RadioButton zhuanzhen_no;
	private RadioButton zhuanzhen_yes;
	private LinearLayout zhuanzhen_layout;
	String isREFERRAL="DM01-11_1";//是否转诊
	EditText jiankangjianyi;//健康评价
//	private EditText nice_spinner_cc1;
//	private EditText nice_spinner_cc2;
//	private EditText nice_spinner_cc3;
//	private EditText nice_spinner_yl1;
//	private EditText nice_spinner_yl2;
//	private EditText nice_spinner_yl3;
	private static final int DRUG_NAME1 = 0x0001;
	private static final int DRUG_NAME2 = 0x0002;
	private static final int DRUG_NAME3 = 0x0003;
	private static final int DRUG_NAME4 = 0x0004;
	private static final int DRUG_NAME11 = 0x0005;
	private static final int DRUG_NAME21 = 0x0006;
	private static final int DRUG_NAME31 = 0x0007;
	private static final int DRUG_NAME41 = 0x0008;
    String strWhere = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'";

	private Button bt_paizhao;
	private ImageView iv_paizhao_img;

    private TextView tv_drug_del1;
    private TextView tv_drug_del2;
    private TextView tv_drug_del3;
    private TextView tv_drug_del4;
    private TextView tv_drug_qt2;
    private TextView tv_drug_qt3;

	private RadioGroup shifang_group;//是否失访
	private RadioButton shifang_no;
	private RadioButton shifang_yes;

	private RadioGroup sflx_group;//随访类型
	private RadioButton sflx_zc;
	private RadioButton sflx_erci;

	private RadioGroup zhuanzhen_jieguo;//到位 不到位
	private RadioButton jieguo_no;
	private RadioButton jieguo_yes;

	private RadioGroup xybglcs_group;//下一步管理措施
	private RadioButton xybglcs1;
	private RadioButton xybglcs2;
	private RadioButton xybglcs3;
	private RadioButton xybglcs4;

	private EditText et_shifang;
	private EditText ed_zzphone;
	private EditText beizhu;

	String shifoushifang = "DM01-10_1";//是否失访
	String suifangleixing = "FOLLOW_UP_TYPE_1";// 随访类型
	String zhuanzhenjieguo = "ROLLOUT_RESULT_TYPE_1";// 随访类型
	String nextguanli = "NEXT_M_M_1";// 下一步管理措施

	private LinearLayout btn_patientsign;
	private ImageView img_patientsign;

	private List<String> ccList;

	private AutoCompleteTextView ed_ywmc11, ed_ywmc12, ed_ywmc13, ed_ywmc14;
	private AutoCompleteTextView ed_ywmc21, ed_ywmc22, ed_ywmc23, ed_ywmc24;
	private AutoCompleteTextView ed_ywmc31, ed_ywmc32, ed_ywmc33, ed_ywmc34;
	private AutoCompleteTextView ed_ywmc41, ed_ywmc42, ed_ywmc43, ed_ywmc44;
	private TextView tvdrug1, tvdrug2, tvdrug3, tvdrug4;
	private EditText drugTime1, drugTime2, drugTime3, drugTime4;
	private TextView tvdrug01, tvdrug02, tvdrug03, tvdrug04;

	private TextView tv_add_drug, tv_del_drug;
	private RecyclerView drugRecyclerView;
	private SfDrugAdapter sfDrugAdapter;
	// 用药情况数据
	private List<CD_DRUGCONDITION> sfDrugList = new ArrayList<>();

	private int id = -1;
	private String sfDate;
	String sfWhere = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'";

	java.text.DecimalFormat weightdf = new java.text.DecimalFormat("#.0");

	public static TNB_SfFragment newInstance(int id, String sfDate) {
		TNB_SfFragment fragment = new TNB_SfFragment();
		Bundle args = new Bundle();
		args.putInt("id", id);
		args.putString("sfDate", sfDate);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void setupView(View view) {
		if (getArguments() != null) {
			id = getArguments().getInt("id", -1);
			sfDate = getArguments().getString("sfDate", null);
			if (id != -1) {
				sfWhere = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "' and id = '" + id + "'";
			} else {
				if(null!=sfDate&&""!=sfDate){
					sfWhere = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD()
							+ "' and FOLLOWUP_DATE = '" + sfDate + " 00:00:00'";
				}else{
					sfWhere = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "' and id = '0'";
				}

			}
		}
		listerDrugName();
		GlobalConts.setMaxLength(13, ed_xy);
		GlobalConts.setMaxLength(3, ed_rxyl, ed_xcsfmb_xy, ed_ryjl,
				ed_xcsfmb_yj, ed_yd_zq, ed_yd_min, ed_xcsfmb_yd_zq, ed_xcsfmb_yd_min);
		GlobalConts.keep2decimal(ed_xcsfmb_tz, ed_tzzs, ed_tzzs_sf, ed_thxhdb, ed_kfxtz);
		GlobalConts.setMaxLength(6, ed_xcsfmb_tz, ed_tzzs, ed_tzzs_sf, ed_thxhdb, ed_kfxtz);
		GlobalConts.setMaxLength(50, ed_qt, ed_qtyw,ed_qtyw2,ed_qtyw3, ed_zzyy, ed_jgkb);
//		GlobalConts.setMaxLength(25, ed_ywmc1, ed_ywmc2, ed_ywmc3);
		GlobalConts.setMaxLength(4, ed_zs, ed_xcsfmb_zs);
		GlobalConts.checkNum(getActivity(), ed_tz, 300.0, 0, 300.0);
		GlobalConts.checkNum(getActivity(), ed_xcsfmb_tz, 300.0, 0, 300.0);
		zhuanzhen_layout.setVisibility(View.VISIBLE);
		setData();

		// 限制只能输如小数点后一位
		ed_tz.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable edt) {
				try {
					String temp = edt.toString();
					int posDot = temp.indexOf(".");
					if (posDot <= 0)
						return;
					if (temp.length() - posDot - 1 > 1) {
						edt.delete(posDot + 2, posDot + 3);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}

			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				if (ags.size() != 0) {
					if (arg0.toString().isEmpty() || TextUtils.isEmpty(ags.get(0).getHEIGHT())) {
						return;
					}

					double dh = Double.parseDouble(ags.get(0).getHEIGHT().toString()) / 100;
					Log.e("++++++++", ags.get(0).getHEIGHT().toString());
					double dw = Double.parseDouble(ed_tz.getText().toString());

					java.text.DecimalFormat df = new java.text.DecimalFormat("#.0");

					ed_tzzs.setText(df.format(GlobalConts.getBMI(dh, dw)) + "");
				}
			}
		});

		ed_xcsfmb_tz.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable edt) {
				try {
					String temp = edt.toString();
					int posDot = temp.indexOf(".");
					if (posDot <= 0)
						return;
					if (temp.length() - posDot - 1 > 1) {
						edt.delete(posDot + 2, posDot + 3);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}

			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				if (ags.size() != 0) {
					if (arg0.toString().isEmpty() || TextUtils.isEmpty(ags.get(0).getHEIGHT())) {
						return;
					}

					double dh = Double.parseDouble(ags.get(0).getHEIGHT().toString()) / 100;
					Log.e("++++++++", ags.get(0).getHEIGHT().toString());
					double dw = Double.parseDouble(ed_xcsfmb_tz.getText().toString());

					java.text.DecimalFormat df = new java.text.DecimalFormat("#.0");

					ed_tzzs_sf.setText(df.format(GlobalConts.getBMI(dh, dw)) + "");
				}
			}
		});
	}

	@Override
	public View findViews(LayoutInflater inflater) {
		fdbmanger = NovaApplication.fdbmanger;
		userInfo = NovaApplication.getUserInfo();
		ags = fdbmanger.findAllByWhere(ARCHIVE_GENERALCONDITION.class, "IDCARD = '" + userInfo.getIDCARD() + "'");
		cqsf2View = inflater.inflate(R.layout.tnb_sfxx, null);
		initViews(cqsf2View);

		return cqsf2View;
	}

	private void listerDrugName() {
//		tvdrug01.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
//						.putExtra("type","tnb"),DRUG_NAME1);
//			}
//		});
//		tvdrug02.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
//						.putExtra("type","tnb"),DRUG_NAME2);
//			}
//		});
//		tvdrug03.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
//						.putExtra("type","tnb"),DRUG_NAME3);
//			}
//		});

//		ed_qtyw.setFocusable(false);
//		ed_qtyw.setFocusableInTouchMode(false);
//		ed_qtyw.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class),DRUG_NAME4);
//			}
//		});
		tvdrug1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
						.putExtra("type","tnb"),DRUG_NAME11);
			}
		});

		tvdrug2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
						.putExtra("type","tnb"),DRUG_NAME21);
			}
		});

		tvdrug3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
						.putExtra("type","tnb"),DRUG_NAME31);
			}
		});
		tvdrug4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
						.putExtra("type","tnb"),DRUG_NAME41);
			}
		});
	}

	// 患者签名
	private String strPatientSignPath = "";

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
				case DRUG_NAME1:
					try {
						BD_DD_DRUG drug1 = (BD_DD_DRUG)data.getSerializableExtra("bean");
						int position = data.getIntExtra("position", 0);
						CD_DRUGCONDITION cdDrugcondition = sfDrugList.get(position);
						cdDrugcondition.setNAME("("+drug1.getProname()+")"+drug1.getDrugs());
						cdDrugcondition.setEVERYTIMEMG(drug1.getRes1());
						cdDrugcondition.setRES1(drug1.getRes2());
						cdDrugcondition.setRES3(drug1.getUsage());
						cdDrugcondition.setRES4(drug1.getDosage());
						cdDrugcondition.setDAILYTIME(drug1.getUsage() + "/" + drug1.getDosage());
//						sfDrugAdapter.notifyItemChanged(position);
						sfDrugAdapter.setNewData(sfDrugList);
						Log.e(TAG, "onActivityResult: "+ drug1 );
//						ed_ywmc1_mg.setText(drug1.getRes1());
//						nice_spinner_dw1.setText(drug1.getRes2());
//						nice_spinner_cc1.setText(drug1.getUsage());
//						nice_spinner_yl1.setText(drug1.getDosage());
					} catch (NumberFormatException e) {
						e.printStackTrace();
					}
					break;
//				case DRUG_NAME2:
//					BD_DD_DRUG drug2 = (BD_DD_DRUG)data.getSerializableExtra("bean");
//					ed_ywmc2.setText("("+drug2.getProname()+")"+drug2.getDrugs());
//					try {
//						ed_ywmc2_mg.setText(drug2.getRes1());
//						nice_spinner_dw2.setText(notNull(drug2.getRes2()));
//						nice_spinner_cc2.setText(notNull(drug2.getUsage()));
//						nice_spinner_yl2.setText(notNull(drug2.getDosage()));
//					} catch (NumberFormatException e) {
//						e.printStackTrace();
//					}
//					break;
//				case DRUG_NAME3:
//					BD_DD_DRUG drug3 = (BD_DD_DRUG)data.getSerializableExtra("bean");
//					ed_ywmc3.setText("("+drug3.getProname()+")"+drug3.getDrugs());
//					try {
//						ed_ywmc3_mg.setText(drug3.getRes1());
//						nice_spinner_dw3.setText(notNull(drug3.getRes2()));
//						nice_spinner_cc3.setText(notNull(drug3.getUsage()));
//						nice_spinner_yl3.setText(notNull(drug3.getDosage()));
//					} catch (NumberFormatException e) {
//						e.printStackTrace();
//					}
//					break;
				case DRUG_NAME4:
					BD_DD_DRUG drug4 = (BD_DD_DRUG)data.getSerializableExtra("bean");
					ed_qtyw.setText("("+drug4.getProname()+")"+drug4.getDrugs());
					break;
				case REQUEST_CODE_PHOTO:
//					String str_imgpath11 = mPhotoPath;
//
//					Intent intent = new Intent(getActivity(), ClipActivity1.class);
//					intent.putExtra("str_img", str_imgpath11);
//					startActivityForResult(intent, REQUEST_CODE_PHOTO1);

					String path = data.getStringExtra("result"); // 图片地址
					Log.e(TAG, "onActivityResult: " + path );
					Glide.with(getActivity()).load(path).into(iv_paizhao_img);
					mPhotoPath = path;
					break;
				case REQUEST_CODE_PHOTO1:
					iv_paizhao_img.setBackground(new BitmapDrawable(null, ImageTools.readBitmapAutoSize(mPhotoPath)));
					break;
				case REQUEST_CODE_PHOTOqianming:
					if (data.getExtras().containsKey("filePath")) {
						strPatientSignPath = data.getExtras().getString("filePath");
						setSign(strPatientSignPath, img_patientsign);
//						patientsign = "重签";
					}
					break;
				case DRUG_NAME11:
					BD_DD_DRUG drug11 = (BD_DD_DRUG)data.getSerializableExtra("bean");
					ed_ywmc11.setText(notNull(drug11.getTjdrugname()));
					ed_ywmc12.setText(notNull(drug11.getTjyongfa()));
					ed_ywmc13.setText(notNull(drug11.getTjdosage()));
					ed_ywmc14.setText(notNull(drug11.getTjmcjl()));
					Log.e(TAG, "onActivityResult: "+ drug11);
					break;
				case DRUG_NAME21:
					BD_DD_DRUG drug21 = (BD_DD_DRUG)data.getSerializableExtra("bean");
					ed_ywmc21.setText(notNull(drug21.getTjdrugname()));
					ed_ywmc22.setText(notNull(drug21.getTjyongfa()));
					ed_ywmc23.setText(notNull(drug21.getTjdosage()));
					ed_ywmc24.setText(notNull(drug21.getTjmcjl()));
					Log.e(TAG, "onActivityResult: "+ drug21);
					break;
				case DRUG_NAME31:
					BD_DD_DRUG drug31 = (BD_DD_DRUG)data.getSerializableExtra("bean");
					ed_ywmc31.setText(notNull(drug31.getTjdrugname()));
					ed_ywmc32.setText(notNull(drug31.getTjyongfa()));
					ed_ywmc33.setText(notNull(drug31.getTjdosage()));
					ed_ywmc34.setText(notNull(drug31.getTjmcjl()));
					Log.e(TAG, "onActivityResult: "+ drug31);
					break;
				case DRUG_NAME41:
					BD_DD_DRUG drug41 = (BD_DD_DRUG)data.getSerializableExtra("bean");
					ed_ywmc41.setText(notNull(drug41.getTjdrugname()));
					ed_ywmc42.setText(notNull(drug41.getTjyongfa()));
					ed_ywmc43.setText(notNull(drug41.getTjdosage()));
					ed_ywmc44.setText(notNull(drug41.getTjmcjl()));
					Log.e(TAG, "onActivityResult: "+ drug41);
					break;
			}
		}
	}

	private int getListIndex11(String str, List<BD_DD_INFO> bds) {
		if (str != null && !str.equals("")) {

			for (int i = 0; i < bds.size(); i++) {
				if (str.equals(bds.get(i).getDATAINFO())) {
					return i;
				}
			}
		}
		return 0;
	}

	private int getListIndex1(String str, List<String> bds) {
		if (str != null && !str.equals("")) {

			for (int i = 0; i < bds.size(); i++) {
				if (str.equals(bds.get(i))) {
					return i;
				}
			}
		}
		return 0;
	}


	@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
	private void setSign(String imgPath, ImageView v) {
		// String result = "";

		if (!TextUtils.isEmpty(imgPath)) {
			Log.e(TAG, "照片src：" + imgPath);
			File file = new File(imgPath);
			if (file.exists()) {
				// v.setbacImageBitmap(ImageTools.readBitmapAutoSize(imgPath));
				v.setVisibility(View.VISIBLE);
				v.setBackground(new BitmapDrawable(null, ImageTools.readBitmapAutoSize(imgPath)));
			} else {
				v.setImageBitmap(null);
			}
		}
	}

	protected String getThreeTime() {
		int mYear;
		int mMonth;
		int mDay;
		String fmt = null;
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH) + 4;

		String monStr = mMonth + "";
		if (monStr.length() == 1) {
			monStr = "0" + monStr;
		}

		mDay = c.get(Calendar.DAY_OF_MONTH);
		String dayStr = mDay + "";
		if (dayStr.length() == 1) {
			dayStr = "0" + dayStr;
		}

		fmt = "" + mYear + "-" + monStr + "-" + dayStr;
		return fmt;
	}

	/**
	 * 初始化布局
	 * 
	 * @param v
	 * @author 王婷婷
	 * @date 2015年12月21日 上午11:10:55
	 */
	private void initViews(View v) {
//		drugTime1 = v.findViewById(R.id.drugTime1);
//		drugTime2 = v.findViewById(R.id.drugTime2);
//		drugTime3 = v.findViewById(R.id.drugTime3);
		drugTime4 = v.findViewById(R.id.drugTime4);
//		tvdrug01 = v.findViewById(R.id.tvdrug01);
//		tvdrug02 = v.findViewById(R.id.tvdrug02);
//		tvdrug03 = v.findViewById(R.id.tvdrug03);
//		tvdrug04 = v.findViewById(R.id.tvdrug04);
		tvdrug1 = v.findViewById(R.id.tvdrug1);
		tvdrug2 = v.findViewById(R.id.tvdrug2);
		tvdrug3 = v.findViewById(R.id.tvdrug3);
		tvdrug4 = v.findViewById(R.id.tvdrug4);

		ed_ywmc11 = v.findViewById(R.id.ed_ywmc11);
		ed_ywmc12 = v.findViewById(R.id.ed_ywmc12);
		ed_ywmc13 = v.findViewById(R.id.ed_ywmc13);
		ed_ywmc14 = v.findViewById(R.id.ed_ywmc14);

		ed_ywmc21 = v.findViewById(R.id.ed_ywmc21);
		ed_ywmc22 = v.findViewById(R.id.ed_ywmc22);
		ed_ywmc23 = v.findViewById(R.id.ed_ywmc23);
		ed_ywmc24 = v.findViewById(R.id.ed_ywmc24);

		ed_ywmc31 = v.findViewById(R.id.ed_ywmc31);
		ed_ywmc32 = v.findViewById(R.id.ed_ywmc32);
		ed_ywmc33 = v.findViewById(R.id.ed_ywmc33);
		ed_ywmc34 = v.findViewById(R.id.ed_ywmc34);

		ed_ywmc41 = v.findViewById(R.id.ed_ywmc41);
		ed_ywmc42 = v.findViewById(R.id.ed_ywmc42);
		ed_ywmc43 = v.findViewById(R.id.ed_ywmc43);
		ed_ywmc44 = v.findViewById(R.id.ed_ywmc44);

		btn_patientsign = v.findViewById(R.id.btn_patientsign);
		img_patientsign = (ImageView) v.findViewById(R.id.img_patientsign);
		btn_patientsign.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 患者签名btn
				Intent intent = new Intent(getActivity(), QianMingActivity.class);
				// //命名规则： 身份证号_签约时间_用户类型_创建时间时分秒_随机数.png[用户类型： 1 医生 2 患者 3
				// 其他图片]
				intent.putExtra(QianMingActivity.SIGNNAME_HEAD, getPhotoPath());
				startActivityForResult(intent, REQUEST_CODE_PHOTOqianming);
			}
		});
		shifang_group = v.findViewById(R.id.shifang_group);
		shifang_no = v.findViewById(R.id.shifang_no);
		shifang_yes = v.findViewById(R.id.shifang_yes);
		sflx_group = v.findViewById(R.id.sflx_group);
		sflx_zc = v.findViewById(R.id.sflx_zc);
		sflx_erci = v.findViewById(R.id.sflx_erci);
		zhuanzhen_jieguo = v.findViewById(R.id.zhuanzhen_jieguo);
		jieguo_no = v.findViewById(R.id.jieguo_no);
		jieguo_yes = v.findViewById(R.id.jieguo_yes);
		xybglcs_group = v.findViewById(R.id.xybglcs_group);
		xybglcs1 = v.findViewById(R.id.xybglcs1);
		xybglcs2 = v.findViewById(R.id.xybglcs2);
		xybglcs4 = v.findViewById(R.id.xybglcs4);
		xybglcs3 = v.findViewById(R.id.xybglcs3);
		et_shifang = v.findViewById(R.id.et_shifang);
		ed_zzphone = v.findViewById(R.id.ed_zzphone);
		beizhu = v.findViewById(R.id.beizhu);

		bt_paizhao = v.findViewById(R.id.bt_paizhao);
		iv_paizhao_img = v.findViewById(R.id.iv_paizhao_img);
		ed_date = (EditTextWithDate) v.findViewById(R.id.ed_date);
		ed_sfys = (NiceSpinner) v.findViewById(R.id.ed_sfys);
		nice_spinner_szfs = (NiceSpinner) v.findViewById(R.id.nice_spinner_szfs);

		gd_zz = (MyGridView) v.findViewById(R.id.gd_zz);

		ed_xy = (EditText) v.findViewById(R.id.ed_xy);
		btn_choose_xy = (Button) v.findViewById(R.id.btn_choose_xy);

		ed_tz = (EditText) v.findViewById(R.id.ed_tz);
		btn_choose_tz = (Button) v.findViewById(R.id.btn_choose_tz);
		ed_xcsfmb_tz = (EditText) v.findViewById(R.id.ed_xcsfmb_tz);

		ed_tzzs = (EditText) v.findViewById(R.id.ed_tzzs);
		ed_height = (EditText) v.findViewById(R.id.ed_height);
		ed_tzzs_sf = (EditText) v.findViewById(R.id.ed_tzzs_sf);
		ed_qt = (EditText) v.findViewById(R.id.ed_qt);
		ed_rxyl = (EditText) v.findViewById(R.id.ed_rxyl);
		ed_xcsfmb_xy = (EditText) v.findViewById(R.id.ed_xcsfmb_xy);
		ed_ryjl = (EditText) v.findViewById(R.id.ed_ryjl);
		ed_zs = (EditText) v.findViewById(R.id.ed_zs);
		ed_xcsfmb_zs = (EditText) v.findViewById(R.id.ed_xcsfmb_zs);
		ed_kfxtz = (EditText) v.findViewById(R.id.ed_kfxtz);
		btn_choose_kfxtz = (Button) v.findViewById(R.id.btn_choose_kfxtz);
		ed_thxhdb = (EditText) v.findViewById(R.id.ed_thxhdb);

		ed_xcsfmb_yj = (EditText) v.findViewById(R.id.ed_xcsfmb_yj);
		ed_yd_zq = (EditText) v.findViewById(R.id.ed_yd_zq);
		ed_yd_min = (EditText) v.findViewById(R.id.ed_yd_min);
		ed_xcsfmb_yd_zq = (EditText) v.findViewById(R.id.ed_xcsfmb_yd_zq);
		ed_xcsfmb_yd_min = (EditText) v.findViewById(R.id.ed_xcsfmb_yd_min);
		nice_spinner_xltz = (NiceSpinner) v.findViewById(R.id.nice_spinner_xltz);
		nice_spinner_zyxw = (NiceSpinner) v.findViewById(R.id.nice_spinner_zyxw);

		nice_spinner_fyycx = (NiceSpinner) v.findViewById(R.id.nice_spinner_fyycx);
		nice_spinner_zbdmbd = (NiceSpinner) v.findViewById(R.id.nice_spinner_zbdmbd);
//		nice_spinner_cc1 =  v.findViewById(R.id.nice_spinner_cc1);
//		nice_spinner_cc2 =  v.findViewById(R.id.nice_spinner_cc2);
//		nice_spinner_cc3 =  v.findViewById(R.id.nice_spinner_cc3);
//		nice_spinner_yl1 =  v.findViewById(R.id.nice_spinner_yl1);
//		nice_spinner_yl2 =  v.findViewById(R.id.nice_spinner_yl2);
//		nice_spinner_yl3 =  v.findViewById(R.id.nice_spinner_yl3);
		gd_ywblfy = (MyGridView) v.findViewById(R.id.gd_ywblfy);

		nice_spinner_clsffl = (NiceSpinner) v.findViewById(R.id.nice_spinner_clsffl);
//		ed_ywmc1 = (AutoCompleteTextView) v.findViewById(R.id.ed_ywmc1);
//		ed_ywmc1_day = (EditText) v.findViewById(R.id.ed_ywmc1_day);
//		ed_ywmc1_mg = (EditText) v.findViewById(R.id.ed_ywmc1_mg);
//		ed_ywmc2 = (AutoCompleteTextView) v.findViewById(R.id.ed_ywmc2);
//		ed_ywmc2_day = (EditText) v.findViewById(R.id.ed_ywmc2_day);
//		ed_ywmc2_mg = (EditText) v.findViewById(R.id.ed_ywmc2_mg);
//		ed_ywmc3 = (AutoCompleteTextView) v.findViewById(R.id.ed_ywmc3);
//		ed_ywmc3_day = (EditText) v.findViewById(R.id.ed_ywmc3_day);
//		ed_ywmc3_mg = (EditText) v.findViewById(R.id.ed_ywmc3_mg);
		ed_jcrq_intro = (EditText) v.findViewById(R.id.ed_jcrq_intro);
		ed_fzjc = (EditText) v.findViewById(R.id.ed_fzjc);

		ed_fzjc.setText("无");

		ed_qtyw = (AutoCompleteTextView) v.findViewById(R.id.ed_qtyw);
		ed_qtyw2 = (AutoCompleteTextView) v.findViewById(R.id.ed_qtyw2);
		ed_qtyw3 = (AutoCompleteTextView) v.findViewById(R.id.ed_qtyw3);
		ed_qtyw_mg = (EditText) v.findViewById(R.id.ed_qtyw_mg);
		ed_qtyw_mg2 = (EditText) v.findViewById(R.id.ed_qtyw_mg2);
		ed_qtyw_mg3 = (EditText) v.findViewById(R.id.ed_qtyw_mg3);
		ed_zzyy = (EditText) v.findViewById(R.id.ed_zzyy);
		ed_jgkb = (EditText) v.findViewById(R.id.ed_jgkb);
		ed_xcsfsj = (EditTextWithDate) v.findViewById(R.id.ed_xcsfsj);
		ed_jcrq = (EditTextWithDate) v.findViewById(R.id.ed_jcrq);

		nice_spinner_dtfy = (NiceSpinner) v.findViewById(R.id.nice_spinner_dtfy);
		ed_date.setBefore(true);
		ed_jcrq.setBefore(true);

        tv_drug_del1 = (TextView) v.findViewById(R.id.tv_drug_del1);
        tv_drug_del2 = (TextView) v.findViewById(R.id.tv_drug_del2);
        tv_drug_del3 = (TextView) v.findViewById(R.id.tv_drug_del3);
        tv_drug_del4 = (TextView) v.findViewById(R.id.tv_drug_del4);
		tv_drug_qt2 = (TextView) v.findViewById(R.id.tv_drug_qt2);
		tv_drug_qt3 = (TextView) v.findViewById(R.id.tv_drug_qt3);

//        tv_drug_del1.setOnClickListener(v1 -> {
//            ed_ywmc1.setText("");
//        });
//        tv_drug_del2.setOnClickListener(v1 -> {
//            ed_ywmc2.setText("");
//        });
//        tv_drug_del3.setOnClickListener(v1 -> {
//            ed_ywmc3.setText("");
//        });
        tv_drug_del4.setOnClickListener(v1 -> {
            ed_qtyw.setText("");
			ed_qtyw_mg.setText("");
        });
		tv_drug_qt2.setOnClickListener(v1 -> {
			ed_qtyw2.setText("");
			ed_qtyw_mg2.setText("");
		});
		tv_drug_qt3.setOnClickListener(v1 -> {
			ed_qtyw3.setText("");
			ed_qtyw_mg3.setText("");
		});

		ed_xcsfsj.setAfter(true, 0);
		String threeTime = getThreeTime();
		Log.e(TAG,"今日日期："+threeTime);
//		ed_xcsfsj.setText(threeTime);

		zhuanzhen_group=v.findViewById(R.id.zhuanzhen_group);
		zhuanzhen_no=v.findViewById(R.id.zhuanzhen_no);
		zhuanzhen_yes=v.findViewById(R.id.zhuanzhen_yes);
		zhuanzhen_layout=v.findViewById(R.id.zhuanzhen_layout);
		jiankangjianyi=v.findViewById(R.id.jiankangjianyi);
		drugRecyclerView = v.findViewById(R.id.drugRecyclerView);
		tv_add_drug = v.findViewById(R.id.tv_add_drug);
		tv_del_drug = v.findViewById(R.id.tv_del_drug);

		initAdapter();

		// 创建一个ArrayAdapter，封装数组
		ArrayAdapter<String> arr = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line,
				tnb_yws);

//		ed_ywmc1.setAdapter(arr);
//		ed_ywmc2.setAdapter(arr);
//		ed_ywmc3.setAdapter(arr);
		ed_qtyw.setAdapter(arr);
		ed_qtyw2.setAdapter(arr);
		ed_qtyw3.setAdapter(arr);

		rb_cjzc = (RadioButton) v.findViewById(R.id.rb_cjzc);
		rb_jr = (RadioButton) v.findViewById(R.id.rb_jr);
		rb_xs = (RadioButton) v.findViewById(R.id.rb_xs);
		rb_sc1 = (RadioButton) v.findViewById(R.id.rb_sc1);
		rb_sc2 = (RadioButton) v.findViewById(R.id.rb_sc2);
		rb_zc1 = (RadioButton) v.findViewById(R.id.rb_zc1);
		rb_zc2 = (RadioButton) v.findViewById(R.id.rb_zc2);
		rb_yc1 = (RadioButton) v.findViewById(R.id.rb_yc1);
		rb_yc2 = (RadioButton) v.findViewById(R.id.rb_yc2);
		rg_jr = (RadioGroup) v.findViewById(R.id.rg_jr);
		rg_xs = (RadioGroup) v.findViewById(R.id.rg_xs);
//		nice_spinner_dw1 = v.findViewById(R.id.nice_spinner_dw1);
//		nice_spinner_dw2 = v.findViewById(R.id.nice_spinner_dw2);
//		nice_spinner_dw3 = v.findViewById(R.id.nice_spinner_dw3);

		try {
			List<SBCL_DATA> list1 = fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE_NAME = '高压' ORDER BY CL_TIME DESC LIMIT 1");
            List<SBCL_DATA> list2 = fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE_NAME = '低压' ORDER BY CL_TIME DESC LIMIT 1");
			if ((list1 != null && list1.size() > 0) && (list2 != null && list2.size() > 0)) {
                String gy = list1.get(0).getCL_VALUE().replace("mmHg","");
                String dy = list2.get(0).getCL_VALUE().replace("mmHg","");
                ed_xy.setText(gy +"/" + dy);
				int age = GlobalConts.getAge(userInfo.getBIRTHDAY());
				Double ssyd = Double.parseDouble(gy);
				Double szyd = Double.parseDouble(dy);
				if(age<65){
					if(ssyd>=140||szyd>=90){
						//此次随访分类，控制不满意
						nice_spinner_clsffl.setSelectedIndex(2);
					}else{
						//此次随访分类，控制满意
						nice_spinner_clsffl.setSelectedIndex(1);
					}
				}else{
					if(ssyd>=150||szyd>=90){
						//此次随访分类，控制不满意
						nice_spinner_clsffl.setSelectedIndex(2);
					}else{
						//此次随访分类，控制满意
						nice_spinner_clsffl.setSelectedIndex(1);
					}
				}
            }
			List<SBCL_DATA> list4 = fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE_NAME = '体重' ORDER BY CL_TIME DESC LIMIT 1");
			if (list4!=null && list4.size()>0) {
				String tizhong = list4.get(0).getCL_VALUE();
				if(tizhong.contains("lfWeightKg")){
					JSONObject obj = new JSONObject(tizhong);
					Double weight = obj.getDouble("lfWeightKg");
					ed_tz.setText(weightdf.format(weight));
				}else{
					ed_tz.setText(tizhong.replace("Kg",""));
				}
            }
			List<SBCL_DATA> list5 = fdbmanger.findAllByWhere(SBCL_DATA.class,
					strWhere + "AND CL_TYPE_NAME = '身高' ORDER BY CL_TIME DESC LIMIT 1");
			if (list5 != null && list5.size() > 0) {
				ed_height.setText(list5.get(0).getCL_VALUE());
				setBMI(ed_height, ed_tz, ed_tzzs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		nice_spinner_clsffl.addOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if ("控制不满意".equals(ccsffl_list.get(position).getDATAINFO())) {
					try {
						Log.e(TAG, "onItemClick: "+ ed_date.getDate() );
						Calendar calendar = Calendar.getInstance(); //得到日历
						calendar.setTime(dateFormat.parse(ed_date.getText().toString()));//把当前时间赋给日历
						calendar.add(Calendar.DATE, 14);
						ed_xcsfsj.setText(dateFormat.format(calendar.getTime()));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}else if ("控制满意".equals(ccsffl_list.get(position).getDATAINFO())){
					try {
						Log.e(TAG, "onItemClick: "+ ed_date.getDate());
						Calendar calendar = Calendar.getInstance(); //得到日历
						calendar.setTime(dateFormat.parse(ed_date.getText().toString()));//把当前时间赋给日历
						calendar.add(Calendar.MONTH, 3);  //设置为前3月
						ed_xcsfsj.setText(dateFormat.format(calendar.getTime()));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
	}

	private void initAdapter() {
		sfDrugAdapter = new SfDrugAdapter(R.layout.item_sf_drug_list, sfDrugList);
		drugRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
		drugRecyclerView.setAdapter(sfDrugAdapter);

		tv_add_drug.setOnClickListener(v -> {
			CD_DRUGCONDITION cdDrugcondition = new CD_DRUGCONDITION();
			cdDrugcondition.setNAME("");
			cdDrugcondition.setRES1("");
			cdDrugcondition.setRES2("");
			cdDrugcondition.setRES3("");
			cdDrugcondition.setRES4("");
			cdDrugcondition.setEVERYTIMEMG("");
			cdDrugcondition.setDAILYTIME("");

			sfDrugList.add(cdDrugcondition);
			sfDrugAdapter.setNewData(sfDrugList);
			sfDrugAdapter.notifyDataSetChanged();
		});
		tv_del_drug.setOnClickListener(v -> {
			if (sfDrugList.size() > 0) {
				sfDrugList.remove(sfDrugList.size() - 1);
				sfDrugAdapter.setNewData(sfDrugList);
				sfDrugAdapter.notifyDataSetChanged();
			}
		});

		sfDrugAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				if (view.getId() == R.id.tvdrug) {
					startActivityForResult(new Intent(getActivity(), QueryDrugListActivity.class)
									.putExtra("type","tnb")
									.putExtra("position", position)
							, DRUG_NAME1);
				}
			}
		});
	}

	/**
	 * 绑定数据值
	 * 
	 * @param str_where
	 * @param flag
	 * @author 王婷婷
	 * @date 2015年12月17日 上午10:30:30
	 */
	private void bindDdata(String flag, String str_where) {
		List<BD_DD_INFO> listZZs = fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class, str_where, "SORT");

		if (flag.equals("症状")) {
			int size = listZZs.size();
			if (size > 0) {
				List<BD_DD_INFO> bd_zzs = new ArrayList<BD_DD_INFO>();
				for (int i = 0; i < size; i++) {
					String str_id = listZZs.get(i).getID();
					if (str_id.equals("DM04-02_R42.x") || str_id.equals("DM04-02_H93.1")
							|| str_id.equals("DM04-03_999.9")) {

					} else {
						bd_zzs.add(listZZs.get(i));

					}
				}
				bd_zzs.add(listZZs.get(1));
				adapter_zz = new CheckBoxAdapter(getActivity(), bd_zzs);
				gd_zz.setAdapter(adapter_zz);
			}

		} else if (flag.equals("药物不良反应")) {
			adapter_ywblfy = new RadioButtonAdapter(listZZs, getActivity());
			adapter_ywblfy.setOtherMaxLength(50);
			gd_ywblfy.setAdapter(adapter_ywblfy);
		}
	}

	@Override
	public void addListener() {
		btn_choose_xy.setOnClickListener(this);
		btn_choose_tz.setOnClickListener(this);
		btn_choose_kfxtz.setOnClickListener(this);

		bt_paizhao.setOnClickListener(this);

		rb_cjzc.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					rg_jr.setVisibility(View.INVISIBLE);
					rg_xs.setVisibility(View.GONE);
				}
			}
		});
		rb_jr.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					rg_jr.setVisibility(View.VISIBLE);
					rg_xs.setVisibility(View.GONE);
				} else {
					rg_jr.setVisibility(View.GONE);
				}

			}
		});
		rb_xs.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					rg_jr.setVisibility(View.GONE);
					rg_xs.setVisibility(View.VISIBLE);
				} else {
					rg_xs.setVisibility(View.GONE);
				}
			}
		});

//		zhuanzhen_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//			@Override
//			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				if(checkedId==zhuanzhen_no.getId()){
//					zhuanzhen_layout.setVisibility(View.GONE);
//					isREFERRAL="DM01-11_1";
//					ed_zzyy.setText("");
//					ed_jgkb.setText("");
//				}else if(checkedId==zhuanzhen_yes.getId()){
//					isREFERRAL="DM01-11_2";
//					zhuanzhen_layout.setVisibility(View.VISIBLE);
//				}
//			}
//		});


		shifang_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==shifang_no.getId()){
					shifoushifang="DM01-10_1";
				}else if(checkedId==shifang_yes.getId()){
					shifoushifang="DM01-10_2";
				}
			}
		});

		sflx_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==sflx_zc.getId()){
					suifangleixing="FOLLOW_UP_TYPE_1";
				}else if(checkedId==sflx_erci.getId()){
					suifangleixing="FOLLOW_UP_TYPE_2";
				}
			}
		});

		zhuanzhen_jieguo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==jieguo_no.getId()){
					zhuanzhenjieguo="ROLLOUT_RESULT_TYPE_1";
				}else if(checkedId==jieguo_yes.getId()){
					zhuanzhenjieguo="ROLLOUT_RESULT_TYPE_2";
				}
			}
		});

		xybglcs_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==xybglcs1.getId()){
					nextguanli="NEXT_M_M_1";
				}else if(checkedId==xybglcs2.getId()){
					nextguanli="NEXT_M_M_2";
				}else if(checkedId==xybglcs3.getId()){
					nextguanli="NEXT_M_M_3";
				}else if(checkedId==xybglcs4.getId()){
					nextguanli="NEXT_M_M_4";
				}
			}
		});

		changeBMI(ed_height, ed_tz, ed_tzzs);
//		changeBMI(ed_height, ed_xcsfmb_tz, ed_tzzs_sf);
		int age = GlobalConts.getAge(userInfo.getBIRTHDAY());
		ed_xy.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					// EditText失去焦点时的处理逻辑
					String str_xy = ed_xy.getText().toString();
					if (!TextUtils.isEmpty(str_xy)) {
						if (str_xy.contains("/")) {
							if (str_xy.split("/").length == 2) {
								String[] split = str_xy.split("/");
								String ssy = split[0];// 收缩压
								String szy = split[1];// 舒张压
								Double ssyd = Double.parseDouble(ssy);
								Double szyd = Double.parseDouble(szy);
								if(age<65){
									if(ssyd>=140||szyd>=90){
										//此次随访分类，控制不满意
										nice_spinner_clsffl.setSelectedIndex(2);
									}else{
										//此次随访分类，控制满意
										nice_spinner_clsffl.setSelectedIndex(1);
									}
								}else{
									if(ssyd>=150||szyd>=90){
										//此次随访分类，控制不满意
										nice_spinner_clsffl.setSelectedIndex(2);
									}else{
										//此次随访分类，控制满意
										nice_spinner_clsffl.setSelectedIndex(1);
									}
								}
							}
						}
					}
				}
			}
		});
		ed_kfxtz.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					// EditText失去焦点时的处理逻辑
					String kfxt = ed_kfxtz.getText().toString();
					if (!TextUtils.isEmpty(kfxt)) {
						Double kfxtd = Double.parseDouble(kfxt);
						if(kfxtd>=7.0){
							//此次随访分类，控制不满意
							nice_spinner_clsffl.setSelectedIndex(2);
						}else if(kfxtd>=3.9&&kfxtd<=6.9){
							//此次随访分类，控制满意
							nice_spinner_clsffl.setSelectedIndex(1);
						}else{
							nice_spinner_dtfy.setSelectedIndex(2);
							nice_spinner_clsffl.setSelectedIndex(2);
						}

					}
				}
			}
		});
	}

	/**
	 * 身高或者体重改变时，BMI的值随之发生改变
	 **/
	private void changeBMI(final EditText etHeight, final EditText etWeight, final EditText etBMI) {
		etHeight.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!TextUtils.isEmpty(etWeight.getText().toString())) {
					setBMI(etHeight, etWeight, etBMI);
				} else {
					etBMI.setText("");
				}

				if (TextUtils.isEmpty(s.toString())) {
					etBMI.setText("");
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		etWeight.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!TextUtils.isEmpty(etHeight.getText().toString())) {
					setBMI(etHeight, etWeight, etBMI);
				} else {
					etBMI.setText("");
				}
				if (TextUtils.isEmpty(s.toString())) {
					etBMI.setText("");
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	/**
	 * 设置BMI
	 * 
	 * @author 王婷婷
	 * @date 2017年4月5日 上午10:48:51
	 */
	private void setBMI(EditText etHeight, EditText etWeight, EditText etBMI) {
		try {
			if (TextUtils.isEmpty(etHeight.getText().toString()) || TextUtils.isEmpty(etHeight.getText().toString())) {
				etBMI.setText("");
				return;
			}

			double dh = Double.parseDouble(etHeight.getText().toString()) / 100;
//			double dw = Double.parseDouble(etWeight.getText().toString());

			double dw = Math.floor(Double.parseDouble(etWeight.getText().toString()) * 10) / 10;
			dw = Double.parseDouble(weightdf.format(dw));
			java.text.DecimalFormat df = new java.text.DecimalFormat("#.0");
			Double bmi = GlobalConts.getBMI(dh, dw);
			etBMI.setText(df.format(bmi) + "");
			if(bmi>=24){
				Double xctz = dw-3;

				ed_xcsfmb_tz.setText(df.format(xctz));
				ed_tzzs_sf.setText(df.format(GlobalConts.getBMI(dh, xctz))+"");
			}else{
				ed_xcsfmb_tz.setText(etWeight.getText().toString());
				ed_tzzs_sf.setText(df.format(bmi) + "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void setData() {
		GlobalConts.doctorList(ed_sfys);
		bindDdata("症状", "PARENTID='DM04-03'");
		bindDdata("药物不良反应", "PARENTID='DM01-11'");

		bindSpinner("随诊方式", "PARENTID='DM04-15'");
		bindSpinner("低血糖", "PARENTID='DM01-73'");
		bindSpinner("心理情况", "PARENTID='DM01-69'");
		bindSpinner("药物依从性", "PARENTID='DM01-70'");
		bindSpinner("此次随访分类", "PARENTID='DM01-71'");
		bindSpinner("足背部", "PARENTID='DM01-72'");
		bindSpinner("药物单位", "PARENTID='DM04-05'");
		setDBData();
	}

	/**
	 * 设置下拉
	 * 
	 * @param flagName
	 * @param strSql
	 * @author 王婷婷
	 * @date 2015年12月24日 上午9:19:28
	 */

	private void bindSpinner(String flagName, String strSql) {
		List<BD_DD_INFO> sList = new ArrayList<BD_DD_INFO>();
		BD_DD_INFO bd = new BD_DD_INFO();
		bd.setDATAINFO("请选择");
		bd.setID("");
		sList.add(bd);

		List<BD_DD_INFO> sList1 = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
				strSql + " order by cast(SORT as int) asc");
		sList.addAll(sList1);

		if (flagName.equals("心理情况")) {

			int size = sList.size();
			if (size > 0) {
				xltz_list = new ArrayList<BD_DD_INFO>();
				zyxw_list = new ArrayList<BD_DD_INFO>();
				for (int i = 0; i < size; i++) {
					String str_id = sList.get(i).getID();
					if (str_id.equals("DM01-69_1") || str_id.equals("DM01-69_2") || str_id.equals("DM01-69_3")
							|| str_id.equals("")) {
						xltz_list.add(sList.get(i));
						zyxw_list.add(sList.get(i));
					}
				}
			}

			nice_spinner_xltz.attachDataSource(xltz_list);
			nice_spinner_zyxw.attachDataSource(zyxw_list);
		} else if (flagName.equals("药物依从性")) {
			ywycx_list = sList;
			nice_spinner_fyycx.attachDataSource(ywycx_list);
		} else if (flagName.equals("此次随访分类")) {
			int size = sList.size();
			if (size > 0) {
				ccsffl_list = new ArrayList<BD_DD_INFO>();
				for (int i = 0; i < size; i++) {
					String str_id = sList.get(i).getID();
					if (str_id.equals("DM01-71_1") || str_id.equals("DM01-71_2") || str_id.equals("DM01-71_3")
							|| str_id.equals("DM01-71_4") || str_id.equals("")) {
						ccsffl_list.add(sList.get(i));
					}
				}
			}

			nice_spinner_clsffl.attachDataSource(ccsffl_list);
		} else if (flagName.equals("随诊方式")) {
			szfs_list = sList;
			nice_spinner_szfs.attachDataSource(szfs_list);
		} else if (flagName.equals("低血糖")) {
			dtfy_list = sList;
			nice_spinner_dtfy.attachDataSource(dtfy_list);
		} else if (flagName.equals("足背部")) {
			zbdm_list = sList;
			nice_spinner_zbdmbd.attachDataSource(zbdm_list);
		} else if (flagName.equals("药物单位")) {
			ywdw_list = sList1;
//			nice_spinner_dw1.attachDataSource(sList1);
//			nice_spinner_dw2.attachDataSource(sList1);
//			nice_spinner_dw3.attachDataSource(sList1);
		}
	}

	private void setDefaultValue() {
//		adapter_zz.setChecked("DM04-02_000.0");
		adapter_zz.setChecked("DM04-03_000.0");
		ed_rxyl.setText("0");
		ed_xcsfmb_xy.setText("0");
		ed_ryjl.setText("0");
		ed_xcsfmb_yj.setText("0");
		nice_spinner_xltz.setSelectedIndex(2);
		nice_spinner_zyxw.setSelectedIndex(2);
		nice_spinner_dtfy.setSelectedIndex(1);
		ed_qt.setText("无");
		adapter_ywblfy.setChecked("DM01-11_1");

		ed_zs.setText("300");
		ed_xcsfmb_zs.setText("280");
		rb_cjzc.setChecked(true);
	}

	private void setDefaultFollowUpData() {
		// 设置默认高血压随访相同表单(双高)
		try {
			String where = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'";
			if (!TextUtils.isEmpty(sfDate)) {
				where = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD()
						+ "' and FOLLOWUPDATE = '" + sfDate + " 00:00:00'";
			}
			List<CD_HYPERTENSIONFOLLOWUP> gyxItemOne = fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class,
					where, "ID DESC LIMIT 1");
			if (gyxItemOne != null && gyxItemOne.size() > 0) {
                CD_HYPERTENSIONFOLLOWUP gxyFollowup = gyxItemOne.get(0);
				ed_date.setDate(gxyFollowup.getFOLLOWUPDATE());
				ed_xcsfsj.setDate(gxyFollowup.getNEXTFOLLOWUPDATE());
				for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
					if (gxyFollowup.getFOLLOWUPDOCTOR().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
						ed_sfys.setSelectedIndex(i);
					}
				}
//				adapter_zz.setChecked(gxyFollowup.getSYMPTOM());
				if (gxyFollowup.getFOLLOWUPWAY().contains("DM04-04")){
					nice_spinner_szfs.setSelectedIndex(getListIndex(gxyFollowup.getFOLLOWUPWAY().replace("DM04-04","DM04-15"), szfs_list));
				}else{
					nice_spinner_szfs.setSelectedIndex(getListIndex(gxyFollowup.getFOLLOWUPWAY(), szfs_list));
				}

				if ((!TextUtils.isEmpty(gxyFollowup.getHYPERTENSION())
						&& (!TextUtils.isEmpty(gxyFollowup.getHYPOTENSION())))) {
					ed_xy.setText(notNull(gxyFollowup.getHYPERTENSION() + "/" + gxyFollowup.getHYPOTENSION()));
				}
				ed_tz.setText(gxyFollowup.getWEIGHT());
				ed_xcsfmb_tz.setText(gxyFollowup.getWEIGHTTARGET());
//				ed_tzzs.setText(GlobalConts.getFormattedBMI(gxyFollowup.getBMI()));
				ed_height.setText(notNull(gxyFollowup.getHeight()));
//				ed_tzzs_sf.setText(GlobalConts.getFormattedBMI(gxyFollowup.getBMITARGET()));
				// ed_qt.setText(notNull(readFollowup.getPHYSICALSYMPTOMOTHER()));sss

				setBMI(ed_height, ed_tz, ed_tzzs);
				setBMI(ed_height, ed_tz, ed_tzzs_sf);
				ed_qt.setText(gxyFollowup.getPHYSICALSYMPTOMOTHER());
				adapter_zz.setOther(gxyFollowup.getSYMPTOMOTHER());
				ed_rxyl.setText(notNull(gxyFollowup.getDAILYSMOKENUM()));
				ed_xcsfmb_xy.setText(notNull(gxyFollowup.getDAILYSMOKENUMTARGET()));
				ed_ryjl.setText(notNull(gxyFollowup.getDAILYDRINKNUM()));
				ed_xcsfmb_yj.setText(notNull(gxyFollowup.getDAILYDRINKNUMTARGET()));
				ed_yd_zq.setText(notNull(gxyFollowup.getSPORTTIMEPERWEEK()));
				ed_yd_min.setText(notNull(gxyFollowup.getSPORTPERMINUTETIME()));
				ed_xcsfmb_yd_zq.setText(notNull(gxyFollowup.getSPORTTIMESPERWEEKTARGET()));
				ed_xcsfmb_yd_min.setText(notNull(gxyFollowup.getSPORTPERMINUTESTIMETARGET()));
				if(!TextUtils.isEmpty(gxyFollowup.getISREFERRAL())){
					if("DM01-11_1".equals(gxyFollowup.getISREFERRAL())){
						zhuanzhen_no.setChecked(true);
						zhuanzhen_yes.setChecked(false);
//						zhuanzhen_layout.setVisibility(View.GONE);
					}else if("DM01-11_2".equals(gxyFollowup.getISREFERRAL())){
						zhuanzhen_yes.setChecked(true);
						zhuanzhen_no.setChecked(false);

					}
				}
				zhuanzhen_layout.setVisibility(View.VISIBLE);
				if(!TextUtils.isEmpty(gxyFollowup.getRes11())){
					shifoushifang = gxyFollowup.getRes11();
					if("DM01-10_1".equals(gxyFollowup.getRes11())){
						shifang_no.setChecked(true);
						shifang_yes.setChecked(false);
					}else if("DM01-10_2".equals(gxyFollowup.getRes11())){
						shifang_yes.setChecked(true);
						shifang_no.setChecked(false);
					}
				}
				if(!TextUtils.isEmpty(gxyFollowup.getRes13())){
					suifangleixing = gxyFollowup.getRes13();
					if("FOLLOW_UP_TYPE_1".equals(gxyFollowup.getRes13())){
						sflx_zc.setChecked(true);
						sflx_erci.setChecked(false);
					}else if("FOLLOW_UP_TYPE_2".equals(gxyFollowup.getRes13())){
						sflx_erci.setChecked(true);
						sflx_zc.setChecked(false);
					}
				}
				if(!TextUtils.isEmpty(gxyFollowup.getRes15())){
					zhuanzhenjieguo = gxyFollowup.getRes15();
					if("ROLLOUT_RESULT_TYPE_1".equals(gxyFollowup.getRes15())){
						jieguo_no.setChecked(true);
						jieguo_yes.setChecked(false);
					}else if("ROLLOUT_RESULT_TYPE_2".equals(gxyFollowup.getRes15())){
						jieguo_yes.setChecked(true);
						jieguo_no.setChecked(false);
					}
				}
				if(!TextUtils.isEmpty(gxyFollowup.getRes16())){
					nextguanli = gxyFollowup.getRes16();
					if("NEXT_M_M_1".equals(gxyFollowup.getRes16())){
						xybglcs1.setChecked(true);
						xybglcs2.setChecked(false);
						xybglcs3.setChecked(false);
						xybglcs4.setChecked(false);
					}else if("NEXT_M_M_2".equals(gxyFollowup.getRes16())){
						xybglcs1.setChecked(false);
						xybglcs2.setChecked(true);
						xybglcs3.setChecked(false);
						xybglcs4.setChecked(false);
					}else if("NEXT_M_M_3".equals(gxyFollowup.getRes16())){
						xybglcs1.setChecked(false);
						xybglcs2.setChecked(false);
						xybglcs3.setChecked(true);
						xybglcs4.setChecked(false);
					}else if("NEXT_M_M_4".equals(gxyFollowup.getRes16())){
						xybglcs1.setChecked(false);
						xybglcs2.setChecked(false);
						xybglcs3.setChecked(false);
						xybglcs4.setChecked(true);
					}
				}
				et_shifang.setText(notNull(gxyFollowup.getRes12()));
				ed_zzphone.setText(notNull(gxyFollowup.getRes14()));
				beizhu.setText(notNull(gxyFollowup.getRes18()));
				ed_fzjc.setText(notNull(gxyFollowup.getASSISTANTEXAM()));


				jiankangjianyi.setText(notNull(gxyFollowup.getHEALTH_ASSESS()));
				nice_spinner_xltz.setSelectedIndex(getListIndex(gxyFollowup.getPSYCHOADJUSTMENT(), xltz_list));
				nice_spinner_zyxw.setSelectedIndex(getListIndex(gxyFollowup.getOBEYDOCTORBEHAVIOR(), zyxw_list));
				nice_spinner_clsffl.setSelectedIndex(getListIndex(gxyFollowup.getFOLLOWUPTYPE(), ccsffl_list));
				nice_spinner_fyycx.setSelectedIndex(getListIndex(gxyFollowup.getMEDICATIONCOMPLIANCE(), ywycx_list));
				adapter_ywblfy.setChecked(gxyFollowup.getADR(), gxyFollowup.getADR_EX());

				if (!TextUtils.isEmpty(gxyFollowup.getRES4())) {
					Glide.with(getActivity()).load(gxyFollowup.getRES4()).into(iv_paizhao_img);
				}

				ed_zzyy.setText(gxyFollowup.getREFERRALREASON());
				ed_jgkb.setText(gxyFollowup.getREFERRALORG());
				ed_xcsfsj.setDate(gxyFollowup.getNEXTFOLLOWUPDATE());

//				ed_jcrq_intro.setText(notNull(gxyFollowup.getASSISTANTEXAM()));

				if (!TextUtils.isEmpty(gxyFollowup.getRES4())) {
					mPhotoPath = gxyFollowup.getRES4();
					Glide.with(getActivity()).load(gxyFollowup.getRES4()).into(iv_paizhao_img);
				}

				if (!TextUtils.isEmpty(gxyFollowup.getRes17())) {
					strPatientSignPath = gxyFollowup.getRes17();
					setSign(gxyFollowup.getRes17(), img_patientsign);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setDBData() {
		try {
			List<CD_DIABETESFOLLOWUP> list = fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class,
					sfWhere, "ID DESC LIMIT 1");

			ccList = new ArrayList<String>();
			ccList.add("口服");
			ccList.add("直肠用药");
			ccList.add("舌下用药");
			ccList.add("注射用药");
			ccList.add("吸入用药");
			ccList.add("局部用药");

			ccList.add("皮下注射");
			ccList.add("皮内注射");
			ccList.add("其它途径");
			ccList.add("滴眼");
			ccList.add("喷喉");
			ccList.add("滴鼻");
			ccList.add("含化");
			ccList.add("敷伤口");
			ccList.add("擦皮肤");
			ccList.add("胸膜腔用药");
			ccList.add("腹腔用药");
			ccList.add("阴道用药");
			ccList.add("推管内用药");
			ccList.add("气管内用药");
			ccList.add("关节腔内用药");
			ccList.add("肌肉注射");
			ccList.add("静脉注射或静脉滴注");
			ccList.add("其他局部用药");

//			nice_spinner_cc1.attachDataSource(ccList);
//			nice_spinner_cc2.attachDataSource(ccList);
//			nice_spinner_cc3.attachDataSource(ccList);

			List<String> ylList = new ArrayList<String>();
			ylList.add("一日一次");
			ylList.add("一日两次");
			ylList.add("一日三次");
			ylList.add("一日四次");
			ylList.add("一周一次");
			ylList.add("一周两次");
			ylList.add("一周三次");
			ylList.add("一周四次");
			ylList.add("一日两次");
			ylList.add("一周两次");
//			ylList.add("睡前服用");
//			ylList.add("12小时一次");
//			ylList.add("1小时一次");
//			ylList.add("3小时一次");
//			ylList.add("6小时一次");
//			ylList.add("一日三次");
//			ylList.add("一日一次");
//			ylList.add("一日四次");
//			ylList.add("隔日一次");
//			ylList.add("一周一次");
//			ylList.add("一周两次");
//			ylList.add("立即服用");
			ylList.add("其他");
//			nice_spinner_yl1.attachDataSource(ylList);
//			nice_spinner_yl2.attachDataSource(ylList);
//			nice_spinner_yl3.attachDataSource(ylList);

			setDefaultValue();

			if (list.size() > 0) {
				from_followup = 1;

				readFollowup = list.get(list.size() - 1);
				ed_date.setDate(readFollowup.getFOLLOWUP_DATE());
				for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
					if (readFollowup.getFOLLOWUPDOCTOR().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
						ed_sfys.setSelectedIndex(i);
					}

				}
				String symptoms = readFollowup.getSYMPTOM();
				adapter_zz.setChecked(symptoms);

				if (readFollowup.getFOLLOWUPWAY().contains("DM04-04")){
					nice_spinner_szfs.setSelectedIndex(getListIndex(readFollowup.getFOLLOWUPWAY().replace("DM04-04","DM04-15"), szfs_list));
				}else{
					nice_spinner_szfs.setSelectedIndex(getListIndex(readFollowup.getFOLLOWUPWAY(), szfs_list));
				}
				if ((!TextUtils.isEmpty(readFollowup.getHYPERTENSION())
						&& (!TextUtils.isEmpty(readFollowup.getHYPOTENSION())))) {
					ed_xy.setText(notNull(readFollowup.getHYPERTENSION() + "/" + readFollowup.getHYPOTENSION()));
				}
				ed_tz.setText(readFollowup.getWEIGHT());
				ed_xcsfmb_tz.setText(readFollowup.getWEIGHTTARGET());
				ed_height.setText(notNull(readFollowup.getHeight()));
//				ed_tzzs.setText(GlobalConts.getFormattedBMI(readFollowup.getBMI()));
//				ed_tzzs_sf.setText(GlobalConts.getFormattedBMI(readFollowup.getBMITARGET()));

				setBMI(ed_height, ed_tz, ed_tzzs);
				setBMI(ed_height, ed_tz, ed_tzzs_sf);
				// ed_qt.setText(notNull(readFollowup.getPHYSICALSYMPTOMOTHER()));sss
				ed_qt.setText(readFollowup.getPHYSICALSYMPTOMOTHER());
				adapter_zz.setOther(readFollowup.getSYMPTOMOTHER());
				ed_rxyl.setText(notNull(readFollowup.getDAILYSMOKENUM()));
				ed_xcsfmb_xy.setText(notNull(readFollowup.getDAILYSMOKENUMTARGET()));
				ed_ryjl.setText(notNull(readFollowup.getDAILYDRINKNUM()));
				ed_xcsfmb_yj.setText(notNull(readFollowup.getDAILYDRINKNUMTARGET()));
				ed_yd_zq.setText(notNull(readFollowup.getSPORTTIMEPERWEEK()));
				ed_yd_min.setText(notNull(readFollowup.getSPORTPERMINUTETIME()));
				ed_xcsfmb_yd_zq.setText(notNull(readFollowup.getSPORTTIMESPERWEEKTARGET()));
				ed_xcsfmb_yd_min.setText(notNull(readFollowup.getSPORTPERMINUTESTIMETARGET()));
				if(!TextUtils.isEmpty(readFollowup.getISREFERRAL())){
					if("DM01-11_1".equals(readFollowup.getISREFERRAL())){
						zhuanzhen_no.setChecked(true);
						zhuanzhen_yes.setChecked(false);
//						zhuanzhen_layout.setVisibility(View.GONE);
					}else if("DM01-11_2".equals(readFollowup.getISREFERRAL())){
						zhuanzhen_yes.setChecked(true);
						zhuanzhen_no.setChecked(false);
//						zhuanzhen_layout.setVisibility(View.VISIBLE);
					}
				}
				zhuanzhen_layout.setVisibility(View.VISIBLE);
				if(!TextUtils.isEmpty(readFollowup.getRes11())){
					shifoushifang = readFollowup.getRes11();
					if("DM01-10_1".equals(readFollowup.getRes11())){
						shifang_no.setChecked(true);
						shifang_yes.setChecked(false);
					}else if("DM01-10_2".equals(readFollowup.getRes11())){
						shifang_yes.setChecked(true);
						shifang_no.setChecked(false);
					}
				}
				if(!TextUtils.isEmpty(readFollowup.getRes13())){
					suifangleixing = readFollowup.getRes13();
					if("FOLLOW_UP_TYPE_1".equals(readFollowup.getRes13())){
						sflx_zc.setChecked(true);
						sflx_erci.setChecked(false);
					}else if("FOLLOW_UP_TYPE_2".equals(readFollowup.getRes13())){
						sflx_erci.setChecked(true);
						sflx_zc.setChecked(false);
					}
				}
				if(!TextUtils.isEmpty(readFollowup.getRes15())){
					zhuanzhenjieguo = readFollowup.getRes15();
					if("ROLLOUT_RESULT_TYPE_1".equals(readFollowup.getRes15())){
						jieguo_no.setChecked(true);
						jieguo_yes.setChecked(false);
					}else if("ROLLOUT_RESULT_TYPE_2".equals(readFollowup.getRes15())){
						jieguo_yes.setChecked(true);
						jieguo_no.setChecked(false);
					}
				}
				if(!TextUtils.isEmpty(readFollowup.getRes16())){
					nextguanli = readFollowup.getRes16();
					if("NEXT_M_M_1".equals(readFollowup.getRes16())){
						xybglcs1.setChecked(true);
						xybglcs2.setChecked(false);
						xybglcs3.setChecked(false);
						xybglcs4.setChecked(false);
					}else if("NEXT_M_M_2".equals(readFollowup.getRes16())){
						xybglcs1.setChecked(false);
						xybglcs2.setChecked(true);
						xybglcs3.setChecked(false);
						xybglcs4.setChecked(false);
					}else if("NEXT_M_M_3".equals(readFollowup.getRes16())){
						xybglcs1.setChecked(false);
						xybglcs2.setChecked(false);
						xybglcs3.setChecked(true);
						xybglcs4.setChecked(false);
					}else if("NEXT_M_M_4".equals(readFollowup.getRes16())){
						xybglcs1.setChecked(false);
						xybglcs2.setChecked(false);
						xybglcs3.setChecked(false);
						xybglcs4.setChecked(true);
					}
				}
				et_shifang.setText(notNull(readFollowup.getRes12()));
				ed_zzphone.setText(notNull(readFollowup.getRes14()));
				beizhu.setText(notNull(readFollowup.getRes18()));

				ed_ywmc11.setText(notNull(readFollowup.getDrug11()));
				ed_ywmc12.setText(notNull(readFollowup.getDrug12()));
				ed_ywmc13.setText(notNull(readFollowup.getDrug13()));
				ed_ywmc14.setText(notNull(readFollowup.getDrug14()));
				ed_ywmc21.setText(notNull(readFollowup.getDrug21()));
				ed_ywmc22.setText(notNull(readFollowup.getDrug22()));
				ed_ywmc23.setText(notNull(readFollowup.getDrug23()));
				ed_ywmc24.setText(notNull(readFollowup.getDrug24()));
				ed_ywmc31.setText(notNull(readFollowup.getDrug31()));
				ed_ywmc32.setText(notNull(readFollowup.getDrug32()));
				ed_ywmc33.setText(notNull(readFollowup.getDrug33()));
				ed_ywmc34.setText(notNull(readFollowup.getDrug34()));
				ed_ywmc41.setText(notNull(readFollowup.getDrug41()));
				ed_ywmc42.setText(notNull(readFollowup.getDrug42()));
				ed_ywmc43.setText(notNull(readFollowup.getDrug43()));
				ed_ywmc44.setText(notNull(readFollowup.getDrug44()));

				jiankangjianyi.setText(notNull(readFollowup.getHEALTH_ASSESS()));
				nice_spinner_zbdmbd.setSelectedIndex(getListIndex(readFollowup.getDORSALISPEDISPULSE(), zbdm_list));
				nice_spinner_xltz.setSelectedIndex(getListIndex(readFollowup.getPSYCHOADJUSTMENT(), xltz_list));
				nice_spinner_zyxw.setSelectedIndex(getListIndex(readFollowup.getOBEYDOCTORBEHAVIOR(), zyxw_list));
				nice_spinner_clsffl.setSelectedIndex(getListIndex(readFollowup.getFOLLOWUPTYPE(), ccsffl_list));
				nice_spinner_fyycx.setSelectedIndex(getListIndex(readFollowup.getMEDICATIONCOMPLIANCE(), ywycx_list));
				adapter_ywblfy.setChecked(readFollowup.getADR(), readFollowup.getADR_EX());

				if (!TextUtils.isEmpty(readFollowup.getRES4())) {
					Glide.with(getActivity()).load(readFollowup.getRES4()).into(iv_paizhao_img);
				}
				if (!TextUtils.isEmpty(readFollowup.getRes17())) {
					strPatientSignPath = readFollowup.getRes17();
					setSign(readFollowup.getRes17(), img_patientsign);
				}
				List<CD_DRUGCONDITION> drugList = fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
						"IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'"
								+ "AND TYPE = '2' and PARENTID = '" + id + "'");
//				int size = drugList.size();

				sfDrugList = drugList;
				sfDrugAdapter.setNewData(sfDrugList);
//				EditText[] medicals = { ed_ywmc1, ed_ywmc2, ed_ywmc3 };
//				EditText[] times = {nice_spinner_cc1,nice_spinner_cc2,nice_spinner_cc3};
//				EditText[] times2 = {nice_spinner_yl1,nice_spinner_yl2,nice_spinner_yl3};
////				EditText[] times = { ed_ywmc1_day, ed_ywmc2_day, ed_ywmc3_day };
//				EditText[] doses = { ed_ywmc1_mg, ed_ywmc2_mg, ed_ywmc3_mg };
//				if (drugList.size() > 0) {
//					for (int i = 0; i < size; i++) {
//						CD_DRUGCONDITION drug = drugList.get(i);
//						if (null == drug){
//							continue;
//						}
//						medicals[i].setText(notNull(drug.getNAME()));
//						String dailytime = drug.getDAILYTIME();
//						if(null != dailytime && !dailytime.equals("")){
//							if(dailytime.contains("/")){
//								String[] split = dailytime.split("/");
//								if(null != split && !split.equals("")){
//									String s = split[1];
//									if(null != s && !s.equals("")){
//										times[i].setText(notNull(s));
//									}
//									String s1 = split[0];
//									if(null != s1 && !s1.equals("")){
//										times2[i].setText(notNull(s1));
//									}
//								}
//							}
//						}
////						times[i].setText(notNull(drug.getDAILYTIME()));
//						doses[i].setText(notNull(String.valueOf(drug.getEVERYTIMEMG())));
//						if (i == 0) {
//							nice_spinner_dw1.setText(notNull(drug.getRES1()));
//							drugTime1.setText(notNull(drug.getRES2()));
//						} else if (i == 1) {
//							nice_spinner_dw2.setText(notNull(drug.getRES1()));
//							drugTime2.setText(notNull(drug.getRES2()));
//						} else if (i == 2) {
//							nice_spinner_dw3.setText(notNull(drug.getRES1()));
//							drugTime3.setText(notNull(drug.getRES2()));
//						}
//					}
//				}

				ed_zzyy.setText(readFollowup.getREFERRALREASON());
				ed_jgkb.setText(readFollowup.getREFERRALORG());
				ed_xcsfsj.setDate(readFollowup.getNEXTFOLLOWUP_DATE());
				ed_zs.setText(notNull(readFollowup.getSTAPLEFOODDAILYG()));
				ed_xcsfmb_zs.setText(notNull(readFollowup.getSTAPLEFOODDAILYGTARGET()));
				ed_kfxtz.setText(notNull(readFollowup.getFPG()));
				ed_thxhdb.setText(notNull(readFollowup.getHBALC()));
				ed_jcrq_intro.setText(notNull(readFollowup.getASSISTANTEXAM()));
				ed_jcrq.setDate(readFollowup.getEXAMDATE());
				nice_spinner_dtfy.setSelectedIndex(getListIndex(readFollowup.getHYPOGLYCEMIARREACTION(), dtfy_list));
				ed_qtyw.setText(notNull(readFollowup.getINSULINTYPE()));
				ed_qtyw_mg.setText(notNull(readFollowup.getINSULINUSAGE()));

				ed_qtyw2.setText(notNull(readFollowup.getINSULINTYPE2()));
				ed_qtyw_mg2.setText(notNull(readFollowup.getINSULINUSAGE2()));
				ed_qtyw3.setText(notNull(readFollowup.getINSULINTYPE3()));
				ed_qtyw_mg3.setText(notNull(readFollowup.getINSULINUSAGE3()));
				ed_fzjc.setText(notNull(readFollowup.getSubCheck()));

				if ("DM01-72_1".equals(readFollowup.getDORSALISPEDISPULSE())) {
					rb_cjzc.setChecked(true);
				} else if ("DM01-72_2".equals(readFollowup.getDORSALISPEDISPULSE())) {
					rb_jr.setChecked(true);
					rg_jr.setVisibility(View.VISIBLE);
					rg_xs.setVisibility(View.GONE);
					if ("DM01-85_1".equals(readFollowup.getDORSALISPEDISPULSE_Less())) {
						rb_sc1.setChecked(true);
					} else if ("DM01-85_2".equals(readFollowup.getDORSALISPEDISPULSE_Less())) {
						rb_zc1.setChecked(true);
					} else if ("DM01-85_3".equals(readFollowup.getDORSALISPEDISPULSE_Less())) {
						rb_yc1.setChecked(true);
					}
				} else if ("DM01-72_3".equals(readFollowup.getDORSALISPEDISPULSE())) {
					rb_xs.setChecked(true);
					rg_jr.setVisibility(View.GONE);
					rg_xs.setVisibility(View.VISIBLE);
					if ("DM01-85_1".equals(readFollowup.getDORSALISPEDISPULSE_Disa())) {
						rb_sc2.setChecked(true);
					} else if ("DM01-85_2".equals(readFollowup.getDORSALISPEDISPULSE_Disa())) {
						rb_zc2.setChecked(true);
					} else if ("DM01-85_3".equals(readFollowup.getDORSALISPEDISPULSE_Disa())) {
						rb_yc2.setChecked(true);
					}
				} else if (TextUtils.isEmpty(readFollowup.getDORSALISPEDISPULSE())) {

				}

			} else {
				ed_date.setText(dateFormat.format(new Date()));
				setDefaultFollowUpData();
				from_followup = 0;

				for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
					if (userInfo.getCREATED_BY().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
						ed_sfys.setSelectedIndex(i);
					}
				}

				setHistoryDrugData();
			}
		} catch (Exception e) {
			e.printStackTrace();
            setHistoryDrugData();
		}
	}

	public void save() {
		// 随诊方式
		String str_id = szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID();
		Log.w(TAG, "随诊方式：" + str_id);
		if (TextUtils.isEmpty(str_id)) {
			nice_spinner_szfs.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请选择随诊方式");
			return;
		} else {
			nice_spinner_szfs.setBackgroundColor(Color.WHITE);
		}

		// 症状
		String str_zz = adapter_zz.getCheckedIdsStr();
		if (TextUtils.isEmpty(str_zz)) {
			gd_zz.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请选择症状");
			return;
		} else {
			gd_zz.setBackgroundColor(Color.WHITE);
		}

		// 血压
		String str_xy = ed_xy.getText().toString();
		if (TextUtils.isEmpty(str_xy)) {
			ed_xy.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请填写血压");
			return;
		} else {
			if (!str_xy.contains("/")) {
				ed_xy.setBackgroundColor(Color.YELLOW);
				((TNBActivity) getActivity()).showTextDialog("血压数据不合理，保存失败");
				return;
			} else {
				String[] split = str_xy.split("/");
				if (!GlobalConts.isRightNumber("血压", split[0])) {
					ed_xy.setBackgroundColor(Color.YELLOW);
					((TNBActivity) getActivity()).showTextDialog("请输入正确的血压值");
					return;
				} else if (!GlobalConts.isRightNumber("血压", split[1])) {
					ed_xy.setBackgroundColor(Color.YELLOW);
					((TNBActivity) getActivity()).showTextDialog("请输入正确的血压值");
					return;
				}
			}
			ed_xy.setBackgroundColor(Color.WHITE);
		}

		// 体重
		String str_tz = ed_tz.getText().toString();
		if (TextUtils.isEmpty(str_tz)) {
			ed_tz.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请输入体重");
			return;
		} else {
			if (!GlobalConts.isRightNumber("体重", str_tz)) {
				ed_tz.setBackgroundColor(Color.YELLOW);
				((TNBActivity) getActivity()).showTextDialog("体重数据不合理，保存失败！");
				return;
			}
			if(null != str_tz && !str_tz.equals("")){
				Double mweight = Double.valueOf(str_tz);
				if(mweight < 10 || mweight > 300){
					((GXYActivity) getActivity()).showTextDialog("体重数据不合理，保存失败！");
					return;
				}
			}
			ed_tz.setBackgroundColor(Color.WHITE);
		}

		String str_xcsftz = ed_xcsfmb_tz.getText().toString();
		if (!TextUtils.isEmpty(str_xcsftz)) {
			if (!GlobalConts.isRightNumber("体重", str_xcsftz)) {
				ed_xcsfmb_tz.setBackgroundColor(Color.YELLOW);
				((TNBActivity) getActivity()).showTextDialog("体重数据不合理，保存失败！");
				return;
			}
		}
		// 身高
		String str_height = ed_height.getText().toString();
		if (TextUtils.isEmpty(str_height)) {
			ed_height.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请输入身高");
			return;
		} else {
			ed_height.setBackgroundColor(Color.WHITE);
			if (!GlobalConts.isRightNumber("身高", str_height)) {
				ed_height.setBackgroundColor(Color.YELLOW);
				((TNBActivity) getActivity()).showTextDialog("身高数据不合理，保存失败！");
				return;
			}
			if(null != str_height && !str_height.equals("")){
				Double mheight = Double.valueOf(str_height);
				if(mheight < 10 || mheight > 300){
					((GXYActivity) getActivity()).showTextDialog("身高数据不合理，保存失败！");
					return;
				}
			}
		}

		// 体质指数
		String str_tzzs = ed_tzzs.getText().toString();
		if (TextUtils.isEmpty(str_tzzs)) {
			ed_tzzs.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请输入体质指数");
			return;
		} else {
			if (!GlobalConts.isRightNumber("体质指数", str_tzzs)) {
				((TNBActivity) getActivity()).showTextDialog("请输入正确的身高或体重");
				return;
			}
		}

		String tzzs_xc = ed_tzzs_sf.getText().toString().trim();
		if (!TextUtils.isEmpty(tzzs_xc)) {
			if (!GlobalConts.isRightNumber("体质指数", tzzs_xc)) {
				((TNBActivity) getActivity()).showTextDialog("请输入正确的身高或体重");
				return;
			}
		}
		// 空腹血糖
		String str_kfxt = ed_kfxtz.getText().toString();
		if (TextUtils.isEmpty(str_kfxt)) {
			ed_kfxtz.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请输入空腹血糖");
			return;
		} else {
			ed_kfxtz.setBackgroundColor(Color.WHITE);
			if (!GlobalConts.isRightNumber("血糖", str_kfxt)) {
				((TNBActivity) getActivity()).showTextDialog("请输入正确空腹血糖");
				return;
			}
		}

		// 下次随访时间
		String str_xcsfsj = ed_xcsfsj.getText().toString();
		if (TextUtils.isEmpty(str_xcsfsj)) {
			ed_xcsfsj.setBackgroundColor(Color.YELLOW);
			((TNBActivity) getActivity()).showTextDialog("请选择下次随访时间");
			return;
		} else {
			ed_xcsfsj.setBackgroundColor(Color.WHITE);
		}

		followUp = new CD_DIABETESFOLLOWUP();
		saveBase();
		saveTizheng();
		saveLife();
//		saveMedicine();
		followUp.setSYMPTOMOTHER(adapter_zz.getOther());
		followUp.setIDCARD(userInfo.getIDCARD());
		followUp.setLAST_UPDATE_BY(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
		followUp.setLAST_UPDATE_DATE(new Date());
		followUp.setIS_UPDATE("Y");
		followUp.setIS_UPDATE_NEW("Y");

		/*----------------------新添加----------------*/
		followUp.setISREFERRAL(isREFERRAL);
		if(!TextUtils.isEmpty(jiankangjianyi.getText())){
			followUp.setHEALTH_ASSESS(jiankangjianyi.getText().toString());
		}

		followUp.setRes11(shifoushifang);
		followUp.setRes13(suifangleixing);
		followUp.setRes15(zhuanzhenjieguo);
		followUp.setRes16(nextguanli);

		if (!TextUtils.isEmpty(ed_ywmc11.getText().toString().trim())) {
			followUp.setDrug11(notNull(ed_ywmc11.getText().toString().trim()));
			followUp.setDrug12(notNull(ed_ywmc12.getText().toString().trim()));
			followUp.setDrug13(notNull(ed_ywmc13.getText().toString().trim()));
			followUp.setDrug14(notNull(ed_ywmc14.getText().toString().trim()));
		}
		if (!TextUtils.isEmpty(ed_ywmc21.getText().toString().trim())) {
			followUp.setDrug21(notNull(ed_ywmc21.getText().toString().trim()));
			followUp.setDrug22(notNull(ed_ywmc22.getText().toString().trim()));
			followUp.setDrug23(notNull(ed_ywmc23.getText().toString().trim()));
			followUp.setDrug24(notNull(ed_ywmc24.getText().toString().trim()));
		}
		if (!TextUtils.isEmpty(ed_ywmc31.getText().toString().trim())) {
			followUp.setDrug31(notNull(ed_ywmc31.getText().toString().trim()));
			followUp.setDrug32(notNull(ed_ywmc32.getText().toString().trim()));
			followUp.setDrug33(notNull(ed_ywmc33.getText().toString().trim()));
			followUp.setDrug34(notNull(ed_ywmc34.getText().toString().trim()));
		}
		if (!TextUtils.isEmpty(ed_ywmc41.getText().toString().trim())) {
			followUp.setDrug41(notNull(ed_ywmc41.getText().toString().trim()));
			followUp.setDrug42(notNull(ed_ywmc42.getText().toString().trim()));
			followUp.setDrug43(notNull(ed_ywmc43.getText().toString().trim()));
			followUp.setDrug44(notNull(ed_ywmc44.getText().toString().trim()));
		}

		String shifangyuanyin = et_shifang.getText().toString().trim();
		if (!TextUtils.isEmpty(shifangyuanyin)) {
			followUp.setRes12(shifangyuanyin);
		}
		String ed_zzphonetext = ed_zzphone.getText().toString().trim();
		if (!TextUtils.isEmpty(ed_zzphonetext)) {
			followUp.setRes14(ed_zzphonetext);
		}
		String beizhutext = beizhu.getText().toString().trim();
		if (!TextUtils.isEmpty(beizhutext)) {
			followUp.setRes18(beizhutext);
		}


		if (rb_cjzc.isChecked()) {
			followUp.setDORSALISPEDISPULSE("DM01-72_1");
			followUp.setDORSALISPEDISPULSE_Less("");
			followUp.setDORSALISPEDISPULSE_Disa("");
		} else if (rb_jr.isChecked()) {
			followUp.setDORSALISPEDISPULSE("DM01-72_2");
			followUp.setDORSALISPEDISPULSE_Disa("");

			if (rb_sc1.isChecked()) {
				followUp.setDORSALISPEDISPULSE_Less("DM01-85_1");
			} else if (rb_zc1.isChecked()) {
				followUp.setDORSALISPEDISPULSE_Less("DM01-85_2");
			} else if (rb_yc1.isChecked()) {
				followUp.setDORSALISPEDISPULSE_Less("DM01-85_3");
			} else {
				followUp.setDORSALISPEDISPULSE_Less("");
			}

		} else if (rb_xs.isChecked()) {
			followUp.setDORSALISPEDISPULSE("DM01-72_3");
			followUp.setDORSALISPEDISPULSE_Less("");

			if (rb_sc2.isChecked()) {
				followUp.setDORSALISPEDISPULSE_Disa("DM01-85_1");
			} else if (rb_zc2.isChecked()) {
				followUp.setDORSALISPEDISPULSE_Disa("DM01-85_2");
			} else if (rb_yc2.isChecked()) {
				followUp.setDORSALISPEDISPULSE_Disa("DM01-85_3");
			} else {
				followUp.setDORSALISPEDISPULSE_Disa("");
			}
		} else {
			GlobalConts.showTextDialog(getActivity(),"足背动脉搏动不能为空");
			return;
//			followUp.setDORSALISPEDISPULSE("");
		}

		// 拍照
		// mPhotoPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/report/123456.png";
		if (!TextUtils.isEmpty(mPhotoPath)) {
			followUp.setRES4(mPhotoPath);
		}

		if (!TextUtils.isEmpty(strPatientSignPath)) {
			followUp.setRes17(strPatientSignPath);
		}

		String yidaosuZhonglei = ed_qtyw.getText().toString();
		String yidaosuMg = ed_qtyw_mg.getText().toString();
		if (!TextUtils.isEmpty(yidaosuZhonglei)) {
			followUp.setINSULINTYPE(yidaosuZhonglei);
			followUp.setINSULINUSAGE(yidaosuMg);
		}
		String yidaosuZhonglei2 = ed_qtyw2.getText().toString();
		String yidaosuMg2 = ed_qtyw_mg2.getText().toString();
		if (!TextUtils.isEmpty(yidaosuZhonglei2)) {
			followUp.setINSULINTYPE2(yidaosuZhonglei2);
			followUp.setINSULINUSAGE2(yidaosuMg2);
		}

		String yidaosuZhonglei3 = ed_qtyw3.getText().toString();
		String yidaosuMg3 = ed_qtyw_mg3.getText().toString();
		if (!TextUtils.isEmpty(yidaosuZhonglei3)) {
			followUp.setINSULINTYPE3(yidaosuZhonglei3);
			followUp.setINSULINUSAGE3(yidaosuMg3);
		}

		String subcheck = ed_fzjc.getText().toString().trim();
		if (!TextUtils.isEmpty(subcheck)) {
			followUp.setSubCheck(subcheck);
		}

		followUp.setREFERRALREASON(ed_zzyy.getText().toString());
		followUp.setREFERRALORG(ed_jgkb.getText().toString());

		try {
			String edDate = ed_xcsfsj.getText().toString();
			Log.e("下次随访时间",edDate+"***********");
			followUp.setNEXTFOLLOWUP_DATE(dateFormat.parse(edDate));
		}catch (Exception e){

		}
		//处理随访次数
		List<CD_DIABETESFOLLOWUP> list = NovaApplication.fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class, " IDCARD='"+followUp.getIDCARD()+"'");


//		if (from_followup == 1) {
//		if (false) {
		if (id != -1 && id!=0) {
			followUp.setUPDATEORG(NovaApplication.getJgbh());
			followUp.setMODIFIED(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
			if(null!=list){
				followUp.setRES3(list.size()+"");
			}else{
				followUp.setRES3("1");
			}
			fdbmanger.updata(followUp, "IDCARD='" + userInfo.getIDCARD() + "' and id = '" + id + "'");
			// ((TNBActivity) getActivity()).showTextDialog("修改成功");
		} else {
			followUp.setCREATED_DATE(new Date());
			followUp.setCREATEORG(NovaApplication.getJgbh());
			followUp.setCREATER(NovaApplication.getJgrno());
			followUp.setCREATED_BY(NovaApplication.getJgrno());
			if(null!=list){
				followUp.setRES3(list.size()+1+"");
			}else{
				followUp.setRES3("1");
			}
			fdbmanger.save(followUp);
			from_followup = 1;
			List<CD_DIABETESFOLLOWUP> gyxItemOne = fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class,
					strWhere,"ID DESC LIMIT 1");
			if (gyxItemOne != null && gyxItemOne.size() > 0) {
				id = gyxItemOne.get(0).getID();
				followUp.setID(id);
			}
		}
		saveMedicine();
		// ((TNBActivity) getActivity()).showTextDialog("保存成功");
		if (GlobalConts.TYPE_JTYS.equals(NovaApplication.getType())) {
			new AlertDialog.Builder(getActivity()).setTitle("保存成功").setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			}).show();

		} else if (GlobalConts.TYPE_GJJBGGWS.equals(NovaApplication.getType())) {
			((TNBActivity) getActivity()).showTextDialog("保存成功");
		}
	}

	private void saveMedicine() {

		List<CD_DRUGCONDITION> drugList = fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
				"IDCARD = '" + NovaApplication.getUserInfo().getIDCARD()
						+ "' and TYPE='2' and PARENTID = '" + id + "'");
		for (CD_DRUGCONDITION drug : drugList) {
			fdbmanger.delete(drug);
		}
		for (int i = 0; i < sfDrugList.size(); i++) {
			CD_DRUGCONDITION cdDrugcondition = sfDrugList.get(i);
			if (!TextUtils.isEmpty(cdDrugcondition.getNAME())) {
				/** 类型 1高血压 2糖尿病 3精神疾病 4冠心病 5脑卒中 */
				cdDrugcondition.setTYPE("2");
				cdDrugcondition.setPARENTID(id);
				cdDrugcondition.setIDCARD(NovaApplication.getUserInfo().getIDCARD());
				fdbmanger.save(cdDrugcondition);
			}
		}
//		String medicine1 = ed_ywmc1.getText().toString();
//		String yl1 = nice_spinner_yl1.getText().toString();
//		String cc1 = nice_spinner_cc1.getText().toString();
//		String dose1 = ed_ywmc1_mg.getText().toString();
//		String dw1 = nice_spinner_dw1.getText().toString();
//
//		String medicine2 = ed_ywmc2.getText().toString();
//		String yl2 = nice_spinner_yl2.getText().toString();
//		String cc2 = nice_spinner_cc2.getText().toString();
//		String dose2 = ed_ywmc2_mg.getText().toString();
//		String dw2 = nice_spinner_dw2.getText().toString();
//
//		String medicine3 = ed_ywmc3.getText().toString();
//		String yl3 = nice_spinner_yl3.getText().toString();
//		String cc3 = nice_spinner_cc3.getText().toString();
//		String dose3 = ed_ywmc3_mg.getText().toString();
//		String dw3 = nice_spinner_dw3.getText().toString();

		String yidaosuZhonglei = ed_qtyw.getText().toString();
		String yidaosuMg = ed_qtyw_mg.getText().toString();

//		CD_DRUGCONDITION drugCondition1 = new CD_DRUGCONDITION();
//		CD_DRUGCONDITION drugCondition2 = new CD_DRUGCONDITION();
//		CD_DRUGCONDITION drugCondition3 = new CD_DRUGCONDITION();
//		drugCondition1.setPARENTID(id);
//		drugCondition2.setPARENTID(id);
//		drugCondition3.setPARENTID(id);
//		drugCondition1.setIDCARD(NovaApplication.getUserInfo().getIDCARD());
//		drugCondition2.setIDCARD(NovaApplication.getUserInfo().getIDCARD());
//		drugCondition3.setIDCARD(NovaApplication.getUserInfo().getIDCARD());
//		/** 类型 1高血压 2糖尿病 3精神疾病 4冠心病 5脑卒中 */
//		drugCondition1.setTYPE("2");
//		drugCondition2.setTYPE("2");
//		drugCondition3.setTYPE("2");
//		if (!TextUtils.isEmpty(medicine1)) {
//			drugCondition1.setRES0("0");
//			drugCondition1.setNAME(medicine1);
//			drugCondition1.setDAILYTIME(yl1 + "/" + cc1);
//			drugCondition1.setEVERYTIMEMG(dose1);
//			drugCondition1.setRES1(dw1);
//			drugCondition1.setRES2(notNull(drugTime1.getText().toString().trim()));
//			fdbmanger.save(drugCondition1);
//		}
//		if (!TextUtils.isEmpty(medicine2)) {
//			drugCondition2.setRES0("0");
//			drugCondition2.setNAME(medicine2);
//			drugCondition2.setDAILYTIME(yl2 + "/" + cc2);
//			drugCondition2.setEVERYTIMEMG(dose2);
//			drugCondition2.setRES1(dw2);
//			drugCondition2.setRES2(notNull(drugTime2.getText().toString().trim()));
//			fdbmanger.save(drugCondition2);
//		}
//		if (!TextUtils.isEmpty(medicine3)) {
//			drugCondition3.setRES0("0");
//			drugCondition3.setNAME(medicine3);
//			drugCondition3.setDAILYTIME(yl3 + "/" + cc3);
//			drugCondition3.setEVERYTIMEMG(dose3);
//			drugCondition3.setRES1(dw3);
//			drugCondition3.setRES2(notNull(drugTime3.getText().toString().trim()));
//			fdbmanger.save(drugCondition3);
//		}
		if (!TextUtils.isEmpty(yidaosuZhonglei)) {
			followUp.setINSULINTYPE(yidaosuZhonglei);
			followUp.setINSULINUSAGE(yidaosuMg);
		}
		String yidaosuZhonglei2 = ed_qtyw2.getText().toString();
		String yidaosuMg2 = ed_qtyw_mg2.getText().toString();
		if (!TextUtils.isEmpty(yidaosuZhonglei2)) {
			followUp.setINSULINTYPE2(yidaosuZhonglei2);
			followUp.setINSULINUSAGE2(yidaosuMg2);
		}

		String yidaosuZhonglei3 = ed_qtyw3.getText().toString();
		String yidaosuMg3 = ed_qtyw_mg3.getText().toString();
		if (!TextUtils.isEmpty(yidaosuZhonglei3)) {
			followUp.setINSULINTYPE3(yidaosuZhonglei3);
			followUp.setINSULINUSAGE3(yidaosuMg3);
		}

		String subcheck = ed_fzjc.getText().toString().trim();
		if (!TextUtils.isEmpty(subcheck)) {
			followUp.setSubCheck(subcheck);
		}

		followUp.setREFERRALREASON(ed_zzyy.getText().toString());
		followUp.setREFERRALORG(ed_jgkb.getText().toString());

		try {
			String edDate = ed_xcsfsj.getText().toString();
			Log.e("下次随访时间",edDate+"***********");
			followUp.setNEXTFOLLOWUP_DATE(dateFormat.parse(edDate));
		}catch (Exception e){

		}
//		followUp.setNEXTFOLLOWUP_DATE(ed_xcsfsj.getDate());
	}

	private void saveLife() {
		String xiyanliang = ed_rxyl.getText().toString();
		String nextXiyan = ed_xcsfmb_xy.getText().toString();
		String yinJiu = ed_ryjl.getText().toString();
		String nextYinjiu = ed_xcsfmb_yj.getText().toString();
		String sportWeek = ed_yd_zq.getText().toString();
		String sportMin = ed_yd_min.getText().toString();
		String nextSportWeek = ed_xcsfmb_yd_zq.getText().toString();
		String nextSportMin = ed_xcsfmb_yd_min.getText().toString();

		String zhushi = ed_zs.getText().toString();
		String nextZhushi = ed_xcsfmb_zs.getText().toString();
		String kongfuXuetang = ed_kfxtz.getText().toString();
		String tanghua = ed_thxhdb.getText().toString();
//		Date examDate = ed_jcrq.getDate();
		try {
			String examDate = ed_jcrq.getText().toString();
			followUp.setEXAMDATE(dateFormat.parse(examDate));

		}catch (Exception e){

		}

		String examInfo = ed_jcrq_intro.getText().toString();

		followUp.setDAILYSMOKENUM(xiyanliang);
		followUp.setDAILYDRINKNUM(yinJiu);
		followUp.setDAILYDRINKNUMTARGET(nextYinjiu);
		followUp.setDAILYSMOKENUMTARGET(nextXiyan);
		followUp.setSPORTTIMEPERWEEK(sportWeek);
		followUp.setSPORTTIMESPERWEEKTARGET(nextSportWeek);
		followUp.setSPORTPERMINUTESTIMETARGET(nextSportMin);
		followUp.setSPORTPERMINUTETIME(sportMin);

		followUp.setPSYCHOADJUSTMENT(xltz_list.get(nice_spinner_xltz.getSelectedIndex()).getID());
		followUp.setOBEYDOCTORBEHAVIOR(zyxw_list.get(nice_spinner_zyxw.getSelectedIndex()).getID());
		followUp.setMEDICATIONCOMPLIANCE(ywycx_list.get(nice_spinner_fyycx.getSelectedIndex()).getID());
		followUp.setFOLLOWUPTYPE(ccsffl_list.get(nice_spinner_clsffl.getSelectedIndex()).getID());

		followUp.setHYPOGLYCEMIARREACTION(dtfy_list.get(nice_spinner_dtfy.getSelectedIndex()).getID());
		followUp.setADR(adapter_ywblfy.getChecked());
		followUp.setADR_EX(adapter_ywblfy.getCheckedOther());
		followUp.setSTAPLEFOODDAILYG(zhushi);

		followUp.setSTAPLEFOODDAILYGTARGET(nextZhushi);
		followUp.setFPG(kongfuXuetang);
		followUp.setHBALC(tanghua);
		followUp.setASSISTANTEXAM(examInfo);

	}

	private void saveTizheng() {
		try {
			String tizhong = ed_tz.getText().toString();
			String nextTizhong = ed_xcsfmb_tz.getText().toString();
			String tizhi = ed_tzzs.getText().toString();
			String nextTizhi = ed_tzzs_sf.getText().toString();
			// String other = adapter_zz.getOther();
			String str_xy = ed_xy.getText().toString();
			String extra = ed_qt.getText().toString();
			if (!TextUtils.isEmpty(str_xy)) {
				if (str_xy.contains("/")) {
					if (str_xy.split("/").length == 2) {
						followUp.setHYPERTENSION(str_xy.split("/")[0]);// 收缩压
						followUp.setHYPOTENSION(str_xy.split("/")[1]);// 舒张压
					} else {
						followUp.setHYPERTENSION("");// 收缩压
						followUp.setHYPOTENSION("");// 舒张压
					}
				} else {
					followUp.setHYPERTENSION("");// 收缩压
					followUp.setHYPOTENSION("");// 舒张压
				}
			} else {
				followUp.setHYPERTENSION("");// 收缩压
				followUp.setHYPOTENSION("");// 舒张压
			}
			followUp.setHeight(ed_height.getText().toString());
			followUp.setWEIGHT(tizhong);
			followUp.setBMI(tizhi);
			followUp.setDORSALISPEDISPULSE(zbdm_list.get(nice_spinner_zbdmbd.getSelectedIndex()).getID());
			followUp.setWEIGHTTARGET(nextTizhong);
			followUp.setBMITARGET(nextTizhi);
			followUp.setPHYSICALSYMPTOMOTHER(extra);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void saveBase() {
		try {
			String edDate = ed_date.getText().toString();
			Log.e("随访时间",edDate+"***********");
			followUp.setFOLLOWUP_DATE(dateFormat.parse(edDate));
		}catch (Exception e){

		}
		if (adapter_zz != null) {
			ArrayList<BD_DD_INFO> checkedInfos = adapter_zz.getCheckedBD_DD_INFO();
			String symptom = "";
			for (BD_DD_INFO bdInfo : checkedInfos) {
				if (TextUtils.isEmpty(symptom)) {
					symptom = bdInfo.getID();
				} else {
					symptom = symptom + "," + bdInfo.getID();
				}
			}

			followUp.setSYMPTOM(symptom);
		}
		followUp.setARCHIVEID("");
		followUp.setFOLLOWUPDOCTOR(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
		if (szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID().contains("DM04-04")){
			followUp.setFOLLOWUPWAY(szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID().replace("DM04-04","DM04-15"));
		}else{
			followUp.setFOLLOWUPWAY(szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID());
		}

	}

	private String notNull(String str) {
		if (TextUtils.isEmpty(str)) {
			return "";
		}
		return str;
	}

	private int getListIndex(String str, List<BD_DD_INFO> bds) {
		if (str != null && !str.equals("")) {

			for (int i = 0; i < bds.size(); i++) {
				if (str.equals(bds.get(i).getID())) {
					return i;
				}
			}
		}
		return 0;
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(getActivity(), ExamHistoryActivity.class);
		int id = v.getId();
		if (id == R.id.btn_choose_xy) {
			intent.putExtra("flag", BluetoothConstant.XUEYA_TYPENAME);
			getActivity().startActivityForResult(intent, RequestXueya);
		} else if (id == R.id.btn_choose_tz) {
			intent.putExtra("flag", BluetoothConstant.TIZHONG_TYPENAME);
			getActivity().startActivityForResult(intent, RequestTizhong);
		} else if (id == R.id.btn_choose_kfxtz) {
			intent.putExtra("flag", BluetoothConstant.XUETANG_TYPENAME);
			getActivity().startActivityForResult(intent, RequestXuetang);
		} else if (id == R.id.bt_paizhao){
//			paizhao();
			startActivityForResult(new Intent(getActivity(), CameraActivity.class), REQUEST_CODE_PHOTO);
		}

	}

	@Override
	public void setXueya(String gaoya, String diya, String mailv) {
		if (!TextUtils.isEmpty(gaoya) && !TextUtils.isEmpty(diya)) {
			ed_xy.setText(gaoya + "/" + diya);
		}

	}

	@Override
	public void setTizhong(String value) {
		ed_tz.setText(value);

	}

	@Override
	public void setXuetang(String value) {
		ed_kfxtz.setText(value);
	}


	private final int REQUEST_CODE_PHOTO = 0x1005;
	private final int REQUEST_CODE_PHOTO1 = 0x1006;
	private final int REQUEST_CODE_PHOTOqianming = 0x1007;
	private String mPhotoPath;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
	public void paizhao() {
		mPhotoPath = getPhotoPath();
		File mCurrentPhotoFile = new File(mPhotoPath);
		Intent intentC = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		if (Build.VERSION.SDK_INT >= 24) {
			Uri imageUri = FileProvider.getUriForFile(getActivity(),
					"com.health" + ".fileprovider", mCurrentPhotoFile);
			intentC.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
			intentC.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		} else {
			intentC.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile));
		}
		startActivityForResult(intentC, REQUEST_CODE_PHOTO);
	}

	private String getPhotoPath() {
		String str = "";
		File file = new File(Environment.getExternalStorageDirectory() + "/PublicHealth/images/");
		str = Environment.getExternalStorageDirectory() + "/PublicHealth/images/" + userInfo.getIDCARD() + "_"
				+  "_" + sdf.format(new Date()) + "_"
				+ (int) ((Math.random() * 9 + 1) * 1000) + ".png";
		if (!file.exists()) {
			file.mkdirs();
		}
		return str;
	}

	private void setHistoryDrugData() {
		try {
			List<CD_DIABETESFOLLOWUP> list = fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class,
					strWhere, "ID DESC");
			if (list != null && list.size() > 0) {
				List<CD_DRUGCONDITION> drugList = fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
						"IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'"
								+ "AND TYPE = '2' and PARENTID = '" + list.get(0).getID() + "'");
	//				int size = drugList.size();

				sfDrugList = drugList;
				sfDrugAdapter.setNewData(sfDrugList);

				ed_qtyw.setText(notNull(list.get(0).getINSULINTYPE()));
				ed_qtyw_mg.setText(notNull(list.get(0).getINSULINUSAGE()));

				ed_qtyw2.setText(notNull(list.get(0).getINSULINTYPE2()));
				ed_qtyw_mg2.setText(notNull(list.get(0).getINSULINUSAGE2()));
				ed_qtyw3.setText(notNull(list.get(0).getINSULINTYPE3()));
				ed_qtyw_mg3.setText(notNull(list.get(0).getINSULINUSAGE3()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
